I’m trying to make this script give the player 5 coins every minute but it’s coming up with an error saying attempt to index nil with WaitForChild
local Players = game:GetService("Players")
local player = Players.LocalPlayer
while true do
wait(60)
player:WaitForChild("leaderstats").Coins.Value += 5
end
You are perhaps accessing LocalPlayer through a server script? It’s not possible, you either had to:
WaitForChild your player (works for once player only)
Loop through all players:
while true do
task.wait(60)
for _, v in ipairs(Players:GetChildren()) do
local Stats = player:FindFirstChild("leaderstats")
if Stats then
Stats.Coins.Value += 1
end
end
end
game.Players.PlayerAdded:Connect(function(player)
while true do
task.wait(60)
local cash = player:WaitForChild("leaderstats").Coins.Value
cash = cash + 5
end
end)
Hey! Put this in a server script under ServerScriptService, it’ll do what you want to do!
You can change WAITS_BETWEEN_REWARD to how long you want to wait before rewarding each player.
You can change COINS_TO_REWARD to how many coins you want to reward each player.
This code snippet will reward 5 coins every 60 seconds.
local WAIT_BETWEEN_REWARD = 60 -- seconds you want to wait before giving a coin
local COINS_TO_REWARD = 5 -- how many coins you want to reward every WAIT_BETWEEN_REWARD seconds
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local timeElapsed: number = 0
RunService.Heartbeat:Connect(function(deltaTime: number)
timeElapsed += deltaTime
if timeElapsed >= WAIT_BETWEEN_REWARD then
timeElapsed = 0
for _, player: Player in ipairs(Players:GetPlayers()) do
local leaderstats: Folder | Model? = player:FindFirstChild("leaderstats")
local coinValue: IntValue? = leaderstats and leaderstats:FindFirstChild("Coins")
if not coinValue then
return
end
coinValue.Value += COINS_TO_REWARD
end
end
end)