I keep getting this error and I don't know how to fix it.
player = game.Players:GetPlayers() script.Parent.MouseButton1Click:connect(function() if player:findFirstChild('leaderstats') then if player.leaderstats.Money.Value >= 500 then player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 script.Parent.Parent.Visible = false script.Parent.Parent.Parent.Start.Visible = true else script.Parent.Text = "Not enough money." wait(3) script.Parent.Text = "CONFIRM" script.Parent.Parent.Visible = false end end end)
ERROR: Workspace.Screen.SurfaceGui.Paying.TextButton.Script:4: attempt to call method 'findFirstChild' (a nil value)
BTW: I have tried 'findFirstChild' and 'FindFirstChild'.
This way we can get the player and make independent functions for all of them, like so,
-- Regular Script game.Players.PlayerAdded:connect(function(player) script.Parent.MouseButton1Click:connect(function() if player:findFirstChild('leaderstats') then if player.leaderstats.Money.Value >= 500 then player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 script.Parent.Parent.Visible = false script.Parent.Parent.Parent.Start.Visible = true else script.Parent.Text = "Not enough money." wait(3) script.Parent.Text = "CONFIRM" script.Parent.Parent.Visible = false end end end) end)
Good Luck!
I agree with avectus because he really stated a lot of good content.
The GetChildren(), and GetPlayers() methods do not work for players. You still need to define the player using
game.Players.PlayerAdded:connect(function(plr) end)
That is what you can use!
player = game.Players:GetPlayers()
This will return a TABLE of all players in the game and calling 'FindFirstChild' on a table won't work.
I'm guessing you meant to define 'player' as a single player so use:
player = game.Players.LocalPlayer
However, if this is a normal script then paste your code into a localscript, then place the script in StarterGui.
Your event should read, game.Workspace [find where your button is in workspace] .MouseButton1Click:connect(function()
-- add code here
end