I feel stupid on this one please don't downvote have not scripted in a long time why do I have an error
player = script.Parent.Parent.Parent.Parent.Parent script.Parent.MouseButton1Click:connect(function(PlayerWhoClicked) local gui = game.Players.LocalPlayer.PlayerGui.CookieMain.ClickMe local leader = game.Players.LocalPlayer:WaitForChild("leaderstats") if leader then local Money = game.Players.LocalPlayer.leader:WaitForChild("Money") if Money then Money.Value = Money.Value + 1 end end end)
error = leader is not a valid member of Player?
Several things are wrong here.
1) :WaitForChild
can't return nil
. It just keeps waiting and waiting until it finds something. So the if leader
and if Money
checks are pointless.
2) You define the local variable leader
, but then you don't use it. You say game.Players.LocalPlayer.leader
, but the object is called leaderstats, not leader
3) You don't use player
anywhere in the script... Since this must be a localscript (since you're using LocalPlayer) why not just use game.Players.LocalPlayer
? In that case, only the local player is going to be able to trigger the neven anyway (and PlayerWhoClicked
was bogus anyway)
With those mistakes fixed,
player = game.Players.LocalPlayer script.Parent.MouseButton1Click:connect(function() local gui = player.PlayerGui.CookieMain.ClickMe local leader = player:WaitForChild("leaderstats") local Money = leader:WaitForChild("Money") Money.Value = Money.Value + 1 end)
Really it doesn't make too much sense to be doing the waiting inside of the function, either, since you'll probably want those values elsewhere. Of course, you should also be waiting for the PlayerGui and CookieMain, probably.
player = game.Players.LocalPlayer local gui = player:WaitForChild("PlayerGui"):WaitForChild("CookieMain").ClickMe local leader = player:WaitForChild("leaderstats") local Money = leader:WaitForChild("Money") script.Parent.MouseButton1Click:connect(function() Money.Value = Money.Value + 1 end)
I mean, it's pretty clear, if there error is player.leader
doesn't exist, then it's just that, you haven't made the leaderstats yet.