Roblox · Beágyazott script · Tooling

🌙 Lua Programozás

Lua alapok Roblox fejlesztőknek: szintaxis, moduláris felépítés, coroutine-k és deployment checklist.

Roblox Studio Coroutines Adatkezelés

1. Környezet és telepítés

  1. Telepítsd a hivatalos Lua 5.4 binárisát, vagy használj brew install lua-t.
  2. Roblox esetén töltsd le a Roblox Studio-t és jelentkezz be fejlesztői fiókkal.
  3. VS Code-hoz: sumneko.lua LSP + Rojo plugin strukturált projektekhez.
lua -v
luarocks install luacheck --local

2. Nyelvi alapok

local név = "Réka"
local kor = 17
local aktív = true
local lista = {1, 2, 3}
local profil = {név = "Réka", rang = "builder"}

print(string.format("%s %d éves", profil.név, kor))

Lua-ban minden globális, hacsak nem local-t használsz. Modulokban mindig zárd le a láthatóságot.

3. Függvények és metatable

local function damage(player, value)
  player.health = math.max(player.health - value, 0)
end

local Vector = {}
Vector.__index = Vector

function Vector.new(x, y)
  return setmetatable({x = x, y = y}, Vector)
end

function Vector:len()
  return math.sqrt(self.x * self.x + self.y * self.y)
end

Használj : szintaxist, ha implicit self paraméter kell.

4. Flow kontroll és hibakezelés

for i = 1, #lista do
  print(lista[i])
end

local ok, err = pcall(function()
  veszélyesMűvelet()
end)

if not ok then
  warn("Hiba történt", err)
end

pcall nélkül egy script hiba megállíthatja az egész Roblox szervizt.

5. Roblox-specifikus API

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

Players.PlayerAdded:Connect(function(player)
  print(player.Name .. " belépett")
end)

local Remote = Instance.new("RemoteEvent")
Remote.Name = "CollectReward"
Remote.Parent = ReplicatedStorage

Mindig nevezd el a RemoteEvent-eket beszédesen, és logold, ha hibás adatot kapsz a klienstől.

6. Moduláris felépítés Rojo-val

-- src/Server/Services/ProfileService.lua
local DataStoreService = game:GetService("DataStoreService")
local ProfileService = {}
ProfileService.__index = ProfileService

function ProfileService.new()
  local store = DataStoreService:GetDataStore("profiles")
  return setmetatable({store = store}, ProfileService)
end

function ProfileService:Save(userId, payload)
  local ok, err = pcall(function()
    self.store:SetAsync(userId, payload)
  end)
  if not ok then warn("[ProfileService]", err) end
end

return ProfileService

A Rojo projekt fájl (.project.json) segít, hogy a ScriptService hierarchiát lokálisan tartsd.

7. Coroutine és async pipeline

local HttpService = game:GetService("HttpService")

local function fetchProfile(userId)
  return coroutine.wrap(function()
    local response = HttpService:GetAsync("https://api.powerfull.dev/" .. userId)
    local payload = HttpService:JSONDecode(response)
    coroutine.yield(payload)
  end)
end

local resumeProfile = fetchProfile(1234)
print(resumeProfile().name)

Roblox támogatja a task.spawn, task.defer API-kat; ezek hatékonyabbak, mint a régi coroutine.create.

8. Debug és profilozás

  • print helyett használj dedikált log modult (küldd Discord webhookra).
  • MicroProfiler (Ctrl+F6) a frame-idő vizsgálathoz.
  • Performance tip: remote eseményeknél csak ID-t küldj, ne táblát.
local function trace(tag, ...)
  print(string.format("[%s]", tag), ...)
end
trace("Inventory", "Item hozzáadva", itemId)

9. Deployment checklist

  • Studio → Game Settings → Security: csak szükséges API-k legyenek engedélyezve.
  • Publikálás előtt futtasd le a Test Service Unit testjeit.
  • Állíts be verzió címkét (Semantic Versioning) a release note-okhoz.
  • Backups: DataStore export (DataStore Editor plugin) + Git repo.

10. Mini projekt: érintő kocka

local part = Instance.new("Part")
part.Name = "RewardCube"
part.Anchored = true
part.Position = Vector3.new(0, 5, 0)
part.Parent = workspace

part.Touched:Connect(function(hit)
  local player = Players:GetPlayerFromCharacter(hit.Parent)
  if not player then return end
  part.BrickColor = BrickColor.Random()
  player:LoadCharacter() -- gyors feedback
end)

Teszteld Studio-ban (Play Here), nézd az Output panelt és ellenőrizd a latency-t (Shift+F5).