Why am I getting this error?

I’m trying to get the leaderstats of a player when they click a button but it errors.

button.MouseButton1Click:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats") -- error line
    local cash = leaderstats:WaitForChild("Cash")
    cash.Value += 1
end)

Error:
Attempt to index nil with ‘WaitForChild’

(It’s a server script)

Can anyone help?

I believe it’s because you can’t use MouseButton1Click on a server-sided script.

Also I don’t think MouseButton1Click takes any parameters, so you’d have to declare the player variable outside of it.

2 Likes

If you wanted to fix this issue, fire a remote event on the client when the button is pressed, and on the server, when the remote event is fired, change the player’s leaderstats.

1 Like