วิธีเรียน Lua สำหรับ FiveM ตั้งแต่พื้นฐาน มือใหม่ควรเริ่มตรงไหน
ถ้าอยากเขียน Script FiveM แต่ยังไม่เคยเขียน Code มาก่อน หนึ่งในคำถามแรกที่มักเจอคือ ควรเรียน Lua สำหรับ FiveM อย่างไร และต้องเรียน Lua ให้จบทั้งภาษาก่อนหรือไม่
คำตอบคือ ไม่จำเป็นต้องเรียน Lua ทุกอย่างก่อนเริ่ม FiveM
วิธีที่เหมาะกว่าคือเรียน Lua เฉพาะพื้นฐานที่จำเป็น แล้วนำไปสร้าง Resource FiveM เล็ก ๆ ทันที จากนั้นค่อยเรียนเรื่อง Client, Server, Native, Event, Database และ Framework เพิ่มตามลำดับ
FiveM ปัจจุบันใช้ CfxLua ซึ่งพัฒนาจาก Lua 5.4 ดังนั้นการเรียนควรอ้างอิง Syntax ของ Lua 5.4 และฝึกควบคู่กับ FiveM API
บทความนี้จะจัด Roadmap ตั้งแต่คนที่ไม่เคยเขียน Code ไปจนถึงสามารถสร้าง FiveM Script ด้วยตัวเองได้
① เริ่มจากเข้าใจก่อนว่า Lua FiveM คืออะไร
Lua เป็นภาษา Programming ที่ FiveM รองรับสำหรับการสร้าง Resource
ตัวอย่างไฟล์ที่พบได้บ่อย เช่น
client.lua
server.lua
config.lua
shared.lua
ตัวอย่าง Code Lua ธรรมดา
print('Hello World')
เมื่อใช้กับ FiveM เราจะสามารถเรียก FiveM API และ GTA V Native เพิ่มได้ เช่น
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
print(coords)
ดังนั้นสิ่งที่คุณต้องเรียนจริง ๆ มีสองส่วน
ภาษา Lua
+
ระบบ FiveM
เรียนเพียง Lua อย่างเดียวจะยังเขียน FiveM ไม่ได้เต็มที่ และจำ Function FiveM อย่างเดียวโดยไม่เข้าใจ Lua ก็จะ Debug Script ได้ยาก
② FiveM ใช้ Lua เวอร์ชันอะไร
FiveM ปัจจุบันใช้ CfxLua ซึ่งเป็น Lua 5.4 ที่มีการปรับแต่งเพิ่มเติมสำหรับ CitizenFX
ดังนั้น Tutorial เก่าบางแห่งอาจแตกต่างจากระบบปัจจุบัน
ตัวอย่างเช่น Resource เก่าอาจมี
lua54 'yes'
ใน fxmanifest.lua
แต่ปัจจุบันไม่จำเป็นต้องเปิด Option นี้แล้ว เพราะ Lua 5.3 ถูกยกเลิกและ Lua Script ของ FiveM ใช้ Lua 5.4 เป็นมาตรฐานแล้ว
สำหรับมือใหม่จึงควรเรียนจากตัวอย่าง Lua 5.4 และเอกสาร FiveM ปัจจุบันเป็นหลัก
③ ต้องเรียน Lua ให้จบก่อนเขียน FiveM หรือไม่
ไม่ต้อง
สิ่งที่ควรเรียนก่อนเริ่มสร้าง FiveM Resource มีประมาณนี้
Variable
Data Type
Operator
if / elseif / else
Function
Table
Loop
Scope
nil
ถ้าเข้าใจเรื่องเหล่านี้พอสมควรแล้ว สามารถเริ่ม FiveM ได้เลย
หลังจากนั้นค่อยเรียน Lua เพิ่มจากปัญหาที่พบระหว่างทำ Project
นี่มักได้ผลดีกว่าการอ่าน Lua Reference Manual ตั้งแต่หน้าแรกจนจบโดยไม่เคยเขียน Resource จริง
④ เรียน Variable ก่อน
Variable ใช้สำหรับเก็บข้อมูล
ตัวอย่าง
local playerName = 'John'
local money = 5000
local isPolice = true
ในตัวอย่างนี้
playerName = ข้อความ
money = ตัวเลข
isPolice = Boolean
FiveM Script ใช้ Variable แทบทุกจุด
เช่น
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
ped และ coords ก็คือ Variable
ดังนั้นเรื่องนี้เป็นพื้นฐานที่ต้องเข้าใจให้ดี
⑤ เรียน Data Type ที่จำเป็น
มือใหม่ไม่จำเป็นต้องจำ Type ทุกอย่างทันที
เริ่มจากประเภทที่พบเป็นประจำก่อน
String
ข้อความ
local name = 'John'
Number
ตัวเลข
local money = 5000
Boolean
จริงหรือเท็จ
local isAdmin = false
nil
ไม่มีค่า
local player = nil
Table
เก็บข้อมูลหลายค่า
local player = {
name = 'John',
job = 'police',
money = 5000
}
Function
เก็บ Logic ที่สามารถเรียกใช้งานได้
local function hello()
print('Hello')
end
ถ้าเข้าใจ Type เหล่านี้ คุณจะอ่าน FiveM Lua Script พื้นฐานได้มากขึ้นทันที
⑥ เข้าใจ local ให้ดี
ใน Lua คุณจะเห็นคำว่า
local
บ่อยมาก
ตัวอย่าง
local money = 1000
โดยทั่วไปควรใช้ Local Variable เมื่อไม่จำเป็นต้องแชร์ Variable ออกไปใน Scope กว้าง
ตัวอย่าง
local function calculatePrice()
local price = 500
return price
end
price จะอยู่ภายใน Scope ของ Function นี้
การใช้ local ช่วยให้ Code
เป็นระเบียบ
ลด Variable ชนกัน
อ่านง่าย
ลด Global Variable ที่ไม่จำเป็น
ควรสร้างนิสัยนี้ตั้งแต่เริ่มเรียน
⑦ เรียน Operator
Operator ใช้คำนวณและเปรียบเทียบค่า
คำนวณ
local total = 100 + 200
local remaining = 1000 - 500
เปรียบเทียบ
money >= 1000
หรือ
job == 'police'
ควรรู้เครื่องหมายหลัก ๆ เช่น
+
-
*
/
==
~=
>
<
>=
<=
และ Logical Operator
and
or
not
ตัวอย่าง
if job == 'police' and isOnDuty then
print('Police on duty')
end
Logic แบบนี้พบตลอดเวลาใน FiveM Script
⑧ เรียน if elseif else
Condition เป็นพื้นฐานสำคัญมาก
ตัวอย่าง
local money = 5000
if money >= 5000 then
print('ซื้อได้')
else
print('เงินไม่พอ')
end
ตัวอย่างหลายเงื่อนไข
local money = 5000
if money >= 10000 then
print('เงินเยอะ')
elseif money >= 5000 then
print('เงินเพียงพอ')
else
print('เงินไม่พอ')
end
FiveM ใช้ Condition สำหรับระบบ เช่น
ตรวจ Job
ตรวจเงิน
ตรวจ Item
ตรวจ Distance
ตรวจ Permission
ตรวจ Player State
ตรวจ Vehicle
ถ้า if ยังไม่คล่อง อย่าเพิ่งรีบไป Framework ขนาดใหญ่
⑨ เรียน Function ให้คล่อง
Function คือหนึ่งในเรื่องที่สำคัญที่สุดของ Programming
ตัวอย่าง
local function hello()
print('Hello FiveM')
end
hello()
รับ Parameter ได้
local function giveMoney(amount)
print('Give money:', amount)
end
giveMoney(500)
และ Return ค่าได้
local function add(a, b)
return a + b
end
local result = add(10, 20)
print(result)
ผลคือ
30
เมื่อเข้าใจ Function จะเรียนเรื่องต่อไปได้ง่าย เช่น
Event Handler
Callback
Export
Command
Database Function
เพราะทั้งหมดมีแนวคิดเกี่ยวกับ Function อยู่เบื้องหลัง
⑩ เรียน Table ให้มากเป็นพิเศษ
ถ้ามีเรื่องหนึ่งใน Lua ที่มือใหม่ FiveM ควรให้ความสำคัญมากเป็นพิเศษ นั่นคือ Table
ตัวอย่าง
local player = {
name = 'John',
job = 'police',
money = 5000
}
อ่านค่าด้วย
print(player.name)
print(player.job)
Table สามารถเก็บรายการได้ด้วย
local jobs = {
'police',
'ambulance',
'mechanic'
}
หรือข้อมูลซ้อนกัน
local player = {
name = 'John',
job = {
name = 'police',
grade = 3
}
}
อ่านข้อมูลด้วย
print(player.job.name)
print(player.job.grade)
Script ของ ESX, QBCore, Qbox และ Resource จำนวนมากใช้ Table อย่างหนัก
ถ้า Table ยังไม่คล่อง การอ่าน Framework จะรู้สึกยากมาก
⑪ Lua Array เริ่มจาก 1
สิ่งหนึ่งที่คนมาจาก JavaScript หรือภาษาอื่นมักสับสนคือ Lua Array โดยทั่วไปเริ่ม Index ที่ 1
ตัวอย่าง
local cars = {
'adder',
'sultan',
'buffalo'
}
รถตัวแรกคือ
cars[1]
ไม่ใช่
cars[0]
ดังนั้น
print(cars[1])
จะได้
adder
เรื่องเล็ก ๆ นี้ทำให้เกิด Bug ได้บ่อยสำหรับคนที่คุ้นกับภาษาอื่น
⑫ เรียน Loop
Loop ใช้ทำงานซ้ำ
ตัวอย่าง for
for i = 1, 5 do
print(i)
end
ถ้าต้องการ Loop Table
local jobs = {
'police',
'ambulance',
'mechanic'
}
for index, job in ipairs(jobs) do
print(index, job)
end
สำหรับ Table ที่ใช้ Key สามารถพบ
for key, value in pairs(player) do
print(key, value)
end
FiveM Script ใช้ Loop บ่อย แต่ต้องระวัง Performance โดยเฉพาะ while true do
⑬ เข้าใจ while true do ก่อนใช้
ตัวอย่าง
while true do
print('Running')
end
ไม่ควรใช้แบบนี้ใน FiveM เพราะ Loop ไม่มีการ Yield
FiveM มักใช้ร่วมกับ Thread และ Wait
CreateThread(function()
while true do
Wait(1000)
print('Running')
end
end)
สิ่งที่ควรเข้าใจคือ
CreateThread
↓
สร้าง Coroutine
↓
while true
↓
ทำงานซ้ำ
↓
Wait
↓
Yield
หากเรียน Loop โดยไม่เข้าใจ Wait() มีโอกาสเขียน Resource ที่กิน CPU สูงตั้งแต่แรก
⑭ เรียน nil และวิธีป้องกัน Error
หนึ่งใน Error ที่ Lua Developer เห็นบ่อยคือ
attempt to index a nil value
ตัวอย่าง
local player = nil
print(player.name)
เกิด Error เพราะ player ไม่มีค่า
ควรตรวจสอบก่อน
if player then
print(player.name)
end
หรือ
if player ~= nil then
print(player.name)
end
เมื่อเริ่มใช้ Framework และ Database เรื่อง nil จะพบมากขึ้น เพราะข้อมูลบางอย่างอาจยังโหลดไม่เสร็จหรือไม่มีอยู่จริง
⑮ เรียน String Manipulation
FiveM Script ต้องจัดการข้อความบ่อย เช่น
Player Name
Identifier
Plate
Job
Command Argument
Notification
ต่อ String ได้ด้วย
local name = 'John'
print('Hello ' .. name)
หรือใช้ string.format
local money = 5000
print(string.format('Money: %d', money))
CfxLua ยังรองรับ Method Style บางกรณี เช่น
local text = ('Money: %d'):format(money)
การ Format String เป็นทักษะเล็ก ๆ แต่ใช้บ่อยมาก
⑯ เรียน Lua แล้วให้เริ่ม FiveM Resource ทันที
เมื่อเข้าใจพื้นฐานประมาณนี้แล้ว อย่ารอให้ “เก่ง Lua” ก่อน
ให้สร้าง Resource แรกเลย
ตัวอย่าง Folder
resources/
└── [local]/
└── lua_training/
├── fxmanifest.lua
└── client.lua
สร้าง fxmanifest.lua
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'
และสร้าง
client.lua
จากนั้นใส่
print('Lua training started')
แล้วใน server.cfg
ensure lua_training
ตอนนี้คุณเริ่มนำ Lua ที่เรียนมาใช้กับ FiveM จริงแล้ว
⑰ Project แรกควรเป็น Command
Command เป็น Project สำหรับมือใหม่ที่ดีมาก เพราะเห็นผลทันที
ตัวอย่าง
RegisterCommand('hello', function()
print('Hello FiveM!')
end, false)
หลังจากนั้นลองเพิ่ม Parameter
RegisterCommand('hello', function(source, args)
local name = args[1]
if name then
print('Hello ' .. name)
else
print('Please enter a name')
end
end, false)
ตัวอย่างการเรียก
hello John
ตรงนี้คุณได้ฝึก
Function
Parameter
Table
if
nil
String
พร้อมกันใน Project เดียว
⑱ Project ที่สองเรียน Native
หลังจาก Command ทำได้แล้ว ให้เริ่มเรียก FiveM/GTA Native
ตัวอย่าง
RegisterCommand('coords', function()
local ped = PlayerPedId()
local coords = GetEntityCoords(ped)
print(coords)
end, false)
Code นี้สอนหลายเรื่อง
RegisterCommand
PlayerPedId
Entity
GetEntityCoords
Variable
Vector3
เมื่อเริ่ม Native แล้วคุณจะเห็นความแตกต่างระหว่าง “เรียน Lua” กับ “เรียน FiveM Development” ชัดขึ้น
⑲ เรียน Vector3
CfxLua มี Vector เป็น Data Type ที่ใช้ได้โดยตรง
ตัวอย่าง
local target = vector3(200.0, -800.0, 30.0)
อ่านค่า
print(target.x)
print(target.y)
print(target.z)
สามารถคำนวณ Distance ได้
local playerCoords = GetEntityCoords(PlayerPedId())
local targetCoords = vector3(200.0, -800.0, 30.0)
local distance = #(playerCoords - targetCoords)
print(distance)
Vector สำคัญมากกับ FiveM เพราะระบบเกมจำนวนมากเกี่ยวข้องกับ Position
⑳ Project ที่สามสร้าง Marker
หลังจากรู้ Coordinates แล้ว ลองสร้าง Marker
Concept คือ
กำหนด Coordinates
↓
สร้าง Thread
↓
วาด Marker
↓
ตรวจ Distance
↓
Player เข้าใกล้
↓
ทำ Action
ตรงนี้จะบังคับให้คุณใช้
vector3
Thread
Loop
Wait
Native
Condition
Distance
จึงเป็น Project ฝึกที่ดีมาก
㉑ เรียน Client Script ให้เข้าใจก่อน
FiveM Lua มีทั้ง Client และ Server
Client Script เหมาะกับงาน เช่น
Ped
Vehicle
Controls
Marker
Blip
Camera
Animation
NUI
ตัวอย่าง
local ped = PlayerPedId()
ทำงานใน Client Context
มือใหม่ควรเรียน Client Script พื้นฐานก่อน เพราะเห็นผลในเกมง่ายและเข้าใจได้เร็ว
จากนั้นจึงเรียน Server Script ต่อ
㉒ เรียน Server Script
เพิ่มไฟล์
server.lua
แล้วแก้ Manifest
fx_version 'cerulean'
game 'gta5'
client_script 'client.lua'
server_script 'server.lua'
ใน server.lua
print('Server script started')
ตอนนี้ต้องเข้าใจว่า
client.lua
→ ทำงานบนเครื่อง Player
server.lua
→ ทำงานบน FXServer
แม้เขียนด้วย Lua เหมือนกัน แต่ Environment ไม่เหมือนกัน
㉓ เรียน Event หลัง Client และ Server
เมื่อเข้าใจสองฝั่งแล้ว ให้เรียน Event
ตัวอย่าง Client
RegisterCommand('testserver', function()
TriggerServerEvent('training:hello')
end, false)
Server
RegisterNetEvent('training:hello', function()
print('Received event from client')
end)
Flow คือ
Client
↓
TriggerServerEvent
↓
Network
↓
Server
↓
RegisterNetEvent
↓
Function
เมื่อทำตรงนี้ได้ คุณจะเริ่มสร้าง Script ที่มี Client-Server Logic ได้จริง
㉔ เรียน TriggerClientEvent ต่อทันที
หลังจาก Client → Server แล้วให้เรียนทิศทางกลับ
Server
RegisterCommand('welcome', function(source)
TriggerClientEvent('training:welcome', source)
end, false)
Client
RegisterNetEvent('training:welcome', function()
print('Welcome player!')
end)
ตอนนี้คุณจะเข้าใจ
Client → Server
Server → Client
ซึ่งเป็นแกนสำคัญของ FiveM Script
㉕ อย่าลืมเรียน Security พร้อม Event
ตรงนี้สำคัญมาก
อย่าคิดว่าเพราะ Client ส่ง Event ได้ แปลว่า Server ควรเชื่อข้อมูลทุกอย่างที่ส่งมา
ตัวอย่างแนวคิดที่ไม่ดี
TriggerServerEvent('giveMoney', 1000000)
ถ้า Server รับจำนวนเงินแล้วเพิ่มให้ทันที ผู้เล่นที่สามารถ Trigger Event เองอาจ Abuse ระบบได้
Server ควรเป็นฝ่ายตรวจสอบ
Player
Permission
ราคา
Item
Reward
Distance
State
จำนวนข้อมูล
ดังนั้น Roadmap ที่ดีควรเป็น
Event
↓
Server Validation
↓
Security
ไม่ใช่เรียน Security หลังเปิด Server ไปแล้ว
㉖ เรียน Config Table
FiveM Resource จำนวนมากแยกค่าตั้งออกไว้ใน config.lua
ตัวอย่าง
Config = {}
Config.Price = 500
Config.MarkerDistance = 20.0
Config.Shop = vector3(
25.0,
-1345.0,
29.5
)
จากนั้น Script เรียก
print(Config.Price)
การฝึก Config ช่วยให้เข้าใจ Table และการแยก Code เป็นไฟล์
㉗ เรียน Shared Script
บาง Config ต้องใช้ทั้ง Client และ Server
สามารถกำหนดใน fxmanifest.lua
shared_script 'config.lua'
เช่น
fx_version 'cerulean'
game 'gta5'
shared_script 'config.lua'
client_script 'client.lua'
server_script 'server.lua'
ตอนนี้ config.lua สามารถโหลดในทั้งสอง Context ตามการออกแบบของ Resource
แต่ต้องระวังว่า ข้อมูลที่ส่งไป Client ไม่ควรถือเป็นข้อมูลลับ
อย่าใส่ Password หรือ Secret สำคัญใน Shared/Client Script
㉘ หลัง Event ให้เรียน Export
เมื่อ Resource เริ่มแยกหลายตัว จะเจอ Export
แนวคิดคือ Resource หนึ่งเปิด Function ให้ Resource อื่นใช้
ตัวอย่าง
exports('GetPrice', function()
return 500
end)
Resource อื่นสามารถเรียก Export ตามชื่อ Resource
การเข้าใจ Export จะช่วยให้ออกแบบระบบ Modular ได้ดีขึ้น
㉙ เรียน Database หลังพื้นฐาน Server
อย่าเริ่ม Lua FiveM ด้วย Database ตั้งแต่วันแรก
ควรให้เข้าใจ
Lua
Client
Server
Event
Function
Table
ก่อน
แล้วจึงเรียน
SQL
MySQL / MariaDB
oxmysql
พื้นฐาน SQL ที่ควรรู้ ได้แก่
SELECT
INSERT
UPDATE
DELETE
รวมถึง
WHERE
JOIN
INDEX
PRIMARY KEY
Transaction
ถ้าพื้นฐาน Server ยังไม่เข้าใจ การเรียน Database พร้อม Framework จะซับซ้อนเกินไป
㉚ เรียน NUI หลังเขียน Game Logic ได้
ถ้าเป้าหมายคือสร้าง
Inventory
HUD
Phone
Banking
Garage UI
Job Menu
ต้องเรียน NUI
Technology หลักคือ
HTML
CSS
JavaScript
Lua จะสื่อสารกับ NUI เช่น
Lua
↓
SendNUIMessage
↓
JavaScript
↓
HTML/CSS
และส่งกลับด้วย NUI Callback
อย่าพยายามเรียน Lua + FiveM + React + Database + Framework พร้อมกันตั้งแต่วันแรก
แยกเรียนเป็นขั้นจะเร็วกว่า
㉛ ควรเรียน Framework ตอนไหน
Framework เช่น
ESX
QBCore
Qbox
ควรเรียนหลังเข้าใจ FiveM Core ขั้นพื้นฐานแล้ว
Roadmap ที่เหมาะคือ
Lua
↓
Resource
↓
Client
↓
Server
↓
Native
↓
Event
↓
Database
↓
Framework
หากข้ามทุกอย่างไป Framework ทันที คุณอาจเขียน Code ตามตัวอย่างได้ แต่เวลาเกิด Error จะไม่รู้ว่าปัญหาเกิดจาก Lua, FiveM หรือ Framework
㉜ วิธีอ่าน Script FiveM ให้เก่งขึ้น
เมื่อโหลด Resource ตัวอย่างมา อย่าอ่านทุกไฟล์พร้อมกัน
ให้เริ่มจาก
ขั้นที่ 1 — fxmanifest.lua
ดูว่า Resource โหลดไฟล์อะไร
ขั้นที่ 2 — config.lua
ดูค่าตั้งหลัก
ขั้นที่ 3 — client.lua
ดู Player Interaction
ขั้นที่ 4 — server.lua
ดู Business Logic และ Database
ขั้นที่ 5 — Event
ตามว่า Event ไหนเรียก Event ไหน
ใช้วิธีถามตัวเองว่า
Code เริ่มจากไหน?
↓
ใครเรียก?
↓
ส่งข้อมูลอะไร?
↓
ไป Client หรือ Server?
↓
จบที่ไหน?
นี่จะช่วยให้เข้าใจ Resource ใหญ่ได้เร็วกว่าอ่านจากบรรทัดแรกถึงบรรทัดสุดท้าย
㉝ วิธีฝึก Debug Lua
อย่ากลัว print()
ตัวอย่าง
local money = 5000
print('money:', money)
หาก Function ไม่ทำงาน
local function buyItem()
print('buyItem started')
-- Logic
end
หากต้องการดู Table สามารถใช้เครื่องมือ Serialization ที่เหมาะสมตาม Runtime/Library ที่ใช้อยู่
หัวใจคือหาว่า
Code วิ่งถึงไหน
Variable มีค่าอะไร
Event ถูกเรียกหรือไม่
อย่าเดาสาเหตุ Error โดยไม่มีข้อมูล
㉞ อ่าน Error Message ก่อนแก้ Code
ตัวอย่าง
SCRIPT ERROR
ให้ดูต่อว่า
Resource ไหน
File ไหน
Line ไหน
Error อะไร
ถ้าเจอ
attempt to index a nil value
อย่าเพิ่งแก้ทั้ง Resource
ไปดู Variable ในบรรทัดที่ Error ก่อน
การอ่าน Stack Trace และ Error Message เป็นทักษะที่ควรฝึกตั้งแต่ Project แรก
㉟ ใช้ Documentation แทนการจำ Native
อย่าพยายามจำ GTA/FiveM Native ทั้งหมด
มี Function จำนวนมากเกินไป
สิ่งที่ควรฝึกคือ
รู้ว่าต้องการทำอะไร
↓
ค้น Native ที่เกี่ยวข้อง
↓
อ่าน Parameter
↓
อ่าน Return Value
↓
ตรวจ Client/Server Context
↓
ทดลอง
เช่น ต้องการหาตำแหน่ง Player
อาจใช้
PlayerPedId()
GetEntityCoords()
เมื่อใช้บ่อย ๆ จะจำได้เอง
㊱ อย่า Copy Code โดยไม่เข้าใจ
Copy ตัวอย่างเพื่อศึกษาได้
แต่ทุกครั้งควรถามว่า
Function นี้คืออะไร
Variable นี้มาจากไหน
Event นี้ถูกเรียกจากไหน
Native นี้ทำอะไร
Code นี้ Client หรือ Server
มี Security Check หรือไม่
ถ้า Copy Resource 100 ตัวแต่ไม่เข้าใจ Flow คุณจะยังแก้ Error ยาก
แต่ถ้าสร้าง Resource เล็ก ๆ 10 ตัวด้วยตัวเอง คุณจะเข้าใจพื้นฐานได้ลึกกว่า
㊲ Project ฝึก Lua FiveM 10 ขั้น
ถ้าไม่รู้ว่าจะฝึกอะไร ให้ทำตามนี้
Project 1 — Hello Command
/hello
Project 2 — Coordinates
แสดงตำแหน่ง Player
Project 3 — Marker
สร้าง Marker หนึ่งจุด
Project 4 — Distance
ตรวจว่า Player อยู่ใกล้จุดหรือไม่
Project 5 — Interaction
กด E เมื่ออยู่ใกล้ Marker
Project 6 — Vehicle
สร้าง Command Spawn รถ
Project 7 — Client/Server Event
ส่ง Event ไป Server
Project 8 — Reward System
ให้ Server ตรวจ Logic แล้วให้ Reward จำลอง
Project 9 — Database
บันทึกข้อมูลลง Database
Project 10 — NUI
สร้าง Menu ง่าย ๆ
เมื่อผ่านสิบขั้นนี้จะเข้าใจ FiveM Lua มากกว่าการดู Tutorial อย่างเดียวอย่างชัดเจน
㊳ ตาราง Roadmap เรียน Lua FiveM
| ระดับ | ควรเรียน |
|---|---|
| เริ่มต้น | Variable, Data Type, Operator |
| พื้นฐาน | if, Function, Table, Loop |
| Lua เพิ่มเติม | Scope, nil, String, Module |
| FiveM เริ่มต้น | Resource, fxmanifest.lua |
| Client | Native, Ped, Vehicle, Vector |
| Server | Player, Logic, Validation |
| Networking | Event, Net Event |
| Resource Design | Export, Config, Dependency |
| Database | SQL, MySQL/MariaDB, oxmysql |
| UI | NUI, HTML, CSS, JavaScript |
| ขั้นสูง | Entity, State Bags, OneSync |
| Production | Security, Debug, Profiler, Resmon |
ไม่จำเป็นต้องรอให้จบทุกระดับก่อนสร้าง Project
ควรเขียน Code ควบคู่กันตลอด
㊴ วิธีเรียน Lua FiveM ให้เร็วที่สุด
ใช้หลักง่าย ๆ
20% อ่าน
80% ลงมือเขียน
ตัวอย่างวันนี้เรียน Function
อย่าอ่าน Function อย่างเดียว
ให้สร้าง Command ที่เรียก Function
พรุ่งนี้เรียน Table
ให้สร้าง Config Table
เรียน Event
ให้สร้าง Client กับ Server ที่ส่ง Event จริง
การเปลี่ยนทุก Concept ให้เป็น Project เล็ก ๆ จะช่วยให้จำและเข้าใจได้ดีกว่าท่อง Syntax
㊵ แผนเรียน Lua FiveM สำหรับมือใหม่
สามารถใช้ลำดับนี้เป็นแนวทางได้
ช่วงแรก
เรียน
Variable
Type
if
Function
Table
Loop
และสร้าง Lua Program สั้น ๆ
ช่วงต่อมา
เรียน
Resource
fxmanifest.lua
client.lua
Native
Command
Vector
หลังจากนั้น
เรียน
server.lua
Event
Security
Config
Export
เมื่อพื้นฐานแข็งแล้ว
เรียน
Database
NUI
Framework
Entity
OneSync
State Bags
Optimization
อย่ายึดติดว่าต้องใช้จำนวนวันเท่ากัน เพราะแต่ละคนมีพื้นฐานต่างกัน
ให้ใช้ความสามารถที่ทำได้เป็นตัววัดแทน
คำถามที่พบบ่อยเกี่ยวกับการเรียน Lua FiveM
ไม่เคยเขียน Code มาก่อนเรียน Lua FiveM ได้ไหม
ได้ Lua มี Syntax ที่ค่อนข้างเริ่มต้นง่าย แต่ควรเรียน Programming Basic ก่อนเข้า Framework
ต้องเรียน Lua กี่วันถึงเขียน FiveM ได้
ไม่มีจำนวนวันที่แน่นอน แต่ไม่จำเป็นต้องเก่ง Lua ก่อน สามารถเริ่ม Resource ง่าย ๆ หลังเข้าใจ Variable, Condition, Function และ Table ได้
ควรเรียน Lua หรือ ESX ก่อน
ควรเรียน Lua และ FiveM Core ก่อน แล้วจึงเรียน ESX
Lua FiveM ต้องเรียน OOP ไหม
ไม่จำเป็นสำหรับการเริ่มต้น แต่แนวคิดการจัด Structure, Module และ Data Design จะมีประโยชน์เมื่อ Project ใหญ่ขึ้น
ต้องจำ Native ทั้งหมดไหม
ไม่ต้อง ควรรู้วิธีค้น Documentation และอ่าน Parameter/Return Value
เรียน Lua อย่างเดียวพอไหม
ถ้าจะพัฒนา FiveM จริง ยังต้องเรียน Client/Server, Native, Events, Database, Networking, Security และอาจต้องเรียน HTML/CSS/JavaScript สำหรับ NUI
มือใหม่ควรเริ่มจาก Framework ไหน
อย่าเพิ่งให้ Framework เป็นจุดเริ่มต้น เรียน FiveM Core ให้เข้าใจก่อน แล้วการเลือก ESX, QBCore หรือ Qbox จะง่ายขึ้น
สรุป วิธีเรียน Lua สำหรับ FiveM
วิธีเรียน Lua สำหรับ FiveM ที่มีประสิทธิภาพไม่ใช่การอ่าน Lua ทั้งภาษาจนจบก่อน แต่คือ เรียนพื้นฐานทีละเรื่องแล้วนำไปใช้กับ Resource จริงทันที
ลำดับที่แนะนำคือ
Variable
↓
Data Type
↓
Condition
↓
Function
↓
Table
↓
Loop
↓
Resource
↓
Client Script
↓
Native
↓
Server Script
↓
Events
↓
Security
↓
Database
↓
NUI
↓
Framework
↓
Networking
↓
Optimization
FiveM ปัจจุบันใช้ CfxLua บน Lua 5.4 ดังนั้นควรเลือก Tutorial และตัวอย่าง Code ที่ไม่ล้าสมัย และใช้เอกสาร Cfx.re เป็นแหล่งอ้างอิงหลักเมื่อไม่แน่ใจเกี่ยวกับ API
สำหรับคนที่ติดตามบทเรียน FiveM บน comsiam อย่าตั้งเป้าว่าจะต้องจำ Code ให้ได้ทั้งหมด แต่ควรตั้งเป้าว่าสามารถสร้าง Resource เล็ก ๆ เอง อ่าน Error เป็น และอธิบาย Flow ของ Client กับ Server ได้
เมื่อทำสิ่งเหล่านี้ได้ การต่อยอดไป ESX, QBCore, Qbox, oxmysql, NUI หรือ Script Custom ขนาดใหญ่จะง่ายขึ้นมาก และเนื้อหาบน comsiam สามารถใช้เป็น Roadmap เพื่อค่อย ๆ ต่อจากพื้นฐาน Lua ไปสู่ FiveM Developer ระดับจริงจังได้
Comments
Post a Comment