Lập trình Roblox · Khóa 2: Lập trình Luau cơ bản – Obby
leaderstats – toàn chữ thường!Script trong ServerScriptService:
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" -- đúng tên này! leaderstats.Parent = player local stage = Instance.new("IntValue") stage.Name = "Stage" stage.Value = 0 stage.Parent = leaderstats end)
| Loại | Chứa | Ví dụ |
|---|---|---|
| IntValue | số nguyên | Stage, Coins |
| NumberValue | số thực | thời gian 12.5 giây |
| StringValue | chữ | danh hiệu "Siêu nhân" |
| BoolValue | đúng / sai | đã mở cửa chưa |
Thêm vào Script checkpoint (ServerScriptService):
local Players = game:GetService("Players") for _, checkpoint in ipairs(workspace.Checkpoints:GetChildren()) do local so = tonumber(string.sub(checkpoint.Name, 11)) checkpoint.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter(hit.Parent) if player then local stage = player.leaderstats.Stage if so > stage.Value then stage.Value = so player.RespawnLocation = checkpoint end end end) end
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local soLanChet = Instance.new("IntValue") soLanChet.Name = "Deaths" soLanChet.Parent = leaderstats player.CharacterAdded:Connect(function(character) local humanoid = character:WaitForChild("Humanoid") humanoid.Died:Connect(function() soLanChet.Value = soLanChet.Value + 1 end) end) end)
Tạo leaderstats có cột Stage. Chạm checkpoint nào thì Stage bằng số của checkpoint đó.
Thêm cột Deaths đếm số lần chết. Thử nhảy vào dung nham 3 lần.
Thêm cột Rank (StringValue): Stage 0–2 "Tân binh", 3–5 "Chiến binh", từ 6 "Huyền thoại". Cập nhật mỗi khi Stage đổi (dùng sự kiện stage.Changed).
| Em gặp | Nguyên nhân |
|---|---|
| Không thấy bảng điểm | tên Folder sai: phải là leaderstats (chữ thường) |
leaderstats is not a valid member of Player | code checkpoint chạy khi leaderstats chưa kịp tạo – dùng player:WaitForChild("leaderstats") |
| Deaths tăng 2 lần | gắn Died ở hai Script khác nhau |
| Stage bị lùi | thiếu điều kiện so > stage.Value |
CharacterAdded → humanoid.Died:Connect(...)