Lập trình Roblox · Khóa 2: Lập trình Luau cơ bản – Obby
player.RespawnLocation – nơi hồi sinh của từng người chơiScript đặt trong mỗi checkpoint:
local Players = game:GetService("Players") local checkpoint = script.Parent checkpoint.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter(hit.Parent) if player and player.RespawnLocation ~= checkpoint then player.RespawnLocation = checkpoint print(player.Name .. " đã lưu " .. checkpoint.Name) end end)
player.RespawnLocation – SpawnLocation mà người chơi này sẽ hồi sinh lần sau~= checkpoint để chỉ lưu một lần, không in liên tụclocal Players = game:GetService("Players") local checkpoint = script.Parent checkpoint.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter(hit.Parent) if player and player.RespawnLocation ~= checkpoint then player.RespawnLocation = checkpoint checkpoint.Color = Color3.fromRGB(0, 255, 100) -- xanh: đã lưu local sparkles = Instance.new("Sparkles") sparkles.Parent = checkpoint task.wait(2) sparkles:Destroy() -- xóa hiệu ứng end end)
Script trong ServerScriptService:
local Players = game:GetService("Players") local thuMuc = workspace.Checkpoints for _, checkpoint in ipairs(thuMuc:GetChildren()) do checkpoint.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter(hit.Parent) if player and player.RespawnLocation ~= checkpoint then player.RespawnLocation = checkpoint print(player.Name .. " đã lưu " .. checkpoint.Name) end end) end
thuMuc:GetChildren() – danh sách mọi vật con trong Folderfor _, checkpoint in ipairs(...) – lần lượt từng vậtObby của em có 3 checkpoint. Kiểm tra: chạm checkpoint 2 → rơi dung nham → hồi sinh ở checkpoint 2.
Checkpoint chưa lưu màu xám; khi lưu chuyển xanh lá và phát hiệu ứng Sparkles trong 2 giây.
Người chơi đã tới checkpoint 3 vô tình chạm lại checkpoint 1 thì không bị lùi về checkpoint 1.
Gợi ý: lấy số ở cuối tên: tonumber(string.sub(checkpoint.Name, 11)).
| Em gặp | Nguyên nhân |
|---|---|
| Vào game xuất hiện giữa màn | checkpoint chưa tắt Neutral |
| Chết vẫn về điểm đầu | checkpoint không phải SpawnLocation, hoặc Script không chạy |
| Output in liên tục | thiếu điều kiện ~= checkpoint |
| Nhiều người cùng chơi bị lẫn checkpoint | dùng player.RespawnLocation (của từng người) – đúng rồi! |
player.RespawnLocation = checkpoint khi chạm~= checkpoint để lưu một lầnfor _, cp in ipairs(thuMuc:GetChildren()) do