Lập trình Roblox · Khóa 3: Hệ thống game – xu, cửa hàng
Script BangDiem trong ServerScriptService:
local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local coins = Instance.new("IntValue") coins.Name = "Coins" coins.Value = 0 coins.Parent = leaderstats end)
local CollectionService = game:GetService("CollectionService") local Players = game:GetService("Players") local function ganXu(xu) local daNhat = false xu.Touched:Connect(function(hit) local player = Players:GetPlayerFromCharacter(hit.Parent) if player and not daNhat then daNhat = true player.leaderstats.Coins.Value += 1 xu.Transparency = 1 -- ẩn đồng xu task.wait(10) -- 10 giây sau hồi lại xu.Transparency = 0 daNhat = false end end) end for _, xu in ipairs(CollectionService:GetTagged("Xu")) do ganXu(xu) end
| Viết đầy đủ | Viết tắt |
|---|---|
x = x + 1 | x += 1 |
x = x - 5 | x -= 5 |
x = x * 2 | x *= 2 |
chu = chu .. "!" | chu ..= "!" |
Thêm vào cuối Script:
while true do for _, xu in ipairs(CollectionService:GetTagged("Xu")) do xu.CFrame = xu.CFrame * CFrame.Angles(0, math.rad(4), 0) end task.wait() end
while true do phải đặt cuối cùng – code bên dưới nó không bao giờ chạy.local mauXu = game:GetService("ServerStorage").DongXu -- có sẵn nhãn Xu for i = 1, 30 do local xu = mauXu:Clone() xu.Position = Vector3.new(math.random(-80, 80), 4, math.random(-80, 80)) xu.Parent = workspace.CacDongXu ganXu(xu) -- gắn sự kiện cho xu mới end
ganXu và trước vòng xoay.Rải 20 đồng xu dọc một con đường. Nhặt hết, kiểm tra Coins = 20.
Thêm loại xu thứ hai: nhãn KimCuong, màu xanh ngọc, giá trị 10 xu, hồi lại sau 30 giây.
Khi nhặt xu: phát ra tia lấp lánh (ParticleEmitter hoặc Sparkles) trong 0,5 giây tại chỗ đồng xu.
| Em gặp | Nguyên nhân |
|---|---|
| Nhặt xu không cộng | nhãn sai tên, hoặc leaderstats không có Coins |
| Nhặt một xu được nhiều điểm | thiếu debounce daNhat |
| Xu bị nhân vật đẩy đi | xu chưa Anchored |
| Nhân vật vấp vào xu | tắt CanCollide của xu |
| Xu mới tạo bằng code không nhặt được | quên gọi ganXu(xu) |
CollectionService:GetTagged("Xu") → một Script cho mọi xuganXu)+=, -=, ..=