Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When I check for the amount of money a thing cost in the shop, it gives me this error?

Asked by 2 years ago
Edited 2 years ago

Hello there. I'm making a shop system and when I do a if stament to see if the player has enough money to buy the item it gives me this error.

workspace.Apple.Script:2: attempt to index nil with "leaderstats"

Here's the code I'm using

script.Parent.ClickDetector.MouseClick:Connect(function()
    if game.Players.LocalPlayer.leaderstats.Money.Value >= 3 then -- heres the part im talking about
        game.Players.LocalPlayer.leaderstats.Money.Value =  game.Players.LocalPlayer.leaderstats.Money.Value - 3 -- dont worry its just the text doing that
    end
end)

also this for somereason

game.Players.LocalPlayer.leaderstats.Money.Value = game.Players.LocalPlayer.leaderstats.Money.Value - 3 -- sorry, the text did that
0
Are you running the code in a LocalScript or a Script? appxritixn 2235 — 2y
0
a script IBulid3 4 — 2y

3 answers

Log in to vote
0
Answered by 2 years ago

LocalPlayer doesn't exist on the server so it's returning nil (hence the error). The MouseClick event has a parameter which is the player that clicked it, use that instead.

0
i don't understand... IBulid3 4 — 2y
0
Which part do you not understand? xInfinityBear 1777 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

Try using :FindFirstChild('leaderstats') or :WaitForChild('leaderstats') Maybe that'll work.

Log in to vote
0
Answered by 2 years ago

@xInfinityBear is correct.

Please accept answers so we know you've found help.

Thanks!

Answer this question