I keep getting this error and I don't know how to fix it.
01 | player = game.Players:GetPlayers() |
02 |
03 | script.Parent.MouseButton 1 Click:connect( function () |
04 | if player:findFirstChild( 'leaderstats' ) then |
05 | if player.leaderstats.Money.Value > = 500 then |
06 | player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 |
07 | script.Parent.Parent.Visible = false |
08 | script.Parent.Parent.Parent.Start.Visible = true |
09 | else |
10 | script.Parent.Text = "Not enough money." |
11 | wait( 3 ) |
12 | script.Parent.Text = "CONFIRM" |
13 | script.Parent.Parent.Visible = false |
14 | end |
15 | end |
16 | 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,
01 | -- Regular Script |
02 | game.Players.PlayerAdded:connect( function (player) |
03 | script.Parent.MouseButton 1 Click:connect( function () |
04 | if player:findFirstChild( 'leaderstats' ) then |
05 | if player.leaderstats.Money.Value > = 500 then |
06 | player.leaderstats.Money.Value = player.leaderstats.Money.Value - 500 |
07 | script.Parent.Parent.Visible = false |
08 | script.Parent.Parent.Parent.Start.Visible = true |
09 | else |
10 | script.Parent.Text = "Not enough money." |
11 | wait( 3 ) |
12 | script.Parent.Text = "CONFIRM" |
13 | script.Parent.Parent.Visible = false |
14 | end |
15 | end |
16 | end ) |
17 | 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
1 | game.Players.PlayerAdded:connect( function (plr) |
2 | end ) |
That is what you can use!
1 | 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:
1 | 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