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

What is wrong with this script?

Asked by 9 years ago

Here is my script. It is a simple code that on click, the object will be removed and the player's leaderstats ("Money") will be increased. I can't find anything wrong with it, but some of you might. The error is "Workspace.Handle.ClickDetector.Script:6: attempt to index a nil value" The teapot disappears, BUT the leaderstats remain the same.

local teapot = script.Parent.Parent

script.Parent.MouseClick:connect(function(playerWhoClicked)
teapot.Transparency = 1
teapot.CanCollide = false
playerWhoClicked:FindFirstChild("Money").Value = playerWhoClicked:FindFirstChild("Money").Value + 125
end)

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

The problem is that the 'Money' leaderstat is inside of leaderstats, not the player..

player>leaderstats>Money

Do this;

local teapot = script.Parent.Parent

script.Parent.MouseClick:connect(function(plr)
    teapot.Transparency = 1
    teapot.CanCollide = false
    local money = plr.leaderstats:FindFirstChild('Money')
    if money then --Check if it's there.
        money.Value = money.Value + 125
    end
end
Ad

Answer this question