Leaderstats attempt to index nil

Ello there, I was making a small robbery thing in my free time. I found an error, and as a new scripter, I’m not really sure what to do. My script is below, everytime I use player:WaitForChild(“leaderstats”) or player:FindFirstChild(“leaderstats”) it says attempt to index nil with ‘FindFirstChild’ or ‘WaitForChild’. I also tried saying player.leaderstats, but it just says attempt to index nil with ‘leaderstats’.

What is supposed to happen is when you click on the money, it takes away your reputation by 10 and gives you 25 cash. But the only problem is that the script can’t find the leaderstats.

script.Parent.ClickDetector.MouseClick:Connect(function(click)
local debounce = false
if debounce == false then
debounce = true
local player = game.Players.LocalPlayer
local leaderstats = player:FindFirstChild(“leaderstats”)
leaderstats.Cash.Value = leaderstats.Cash.Value + 25
leaderstats.Reputation.Value = leaderstats.Reputation.Value - 10
wait(2.5)
debounce = false
end
end)

If this is a server script, game.Players.LocalPlayer won’t work.

Ah, I should’ve remembered. But then what should I do, make it a local script?

1 Like

As @Quwanterz mentioned above, LocalPlayer will be nil on Server as the server does not belong to a player. To get the Player who clicked, you just use the argument passed by the MouseClick event, thats is the click. Also would like to point out that the debounce is useless if put inside as it will always be false when u click, put it in the global scope or outside the event.

1 Like

Thanks so much, @Quwanterz and @megatushar100. I removed the game.Players.LocalPlayer local variable, and used the MouseClick event to get the player.

3 Likes