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

Why is this not working?

Asked by 8 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

I keep getting this error and I don't know how to fix it.

01player = game.Players:GetPlayers()
02 
03script.Parent.MouseButton1Click:connect(function()
04if 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
15end
16end)

ERROR: Workspace.Screen.SurfaceGui.Paying.TextButton.Script:4: attempt to call method 'findFirstChild' (a nil value)

BTW: I have tried 'findFirstChild' and 'FindFirstChild'.

3 answers

Log in to vote
1
Answered by 8 years ago

Use the Player Added Event!

This way we can get the player and make independent functions for all of them, like so,

01-- Regular Script
02game.Players.PlayerAdded:connect(function(player)
03    script.Parent.MouseButton1Click: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)
17end)
This wasn't really much of an answer, more of a guess and pointing something out.

Good Luck!

1
YOU COPIED MY ANSWER! Lem0nzzx 70 — 8y
1
Come on man... not cool Avectus 120 — 8y
0
I'm sorry :/. He asked me for help after reviewing your answer Lemon. Like I said in my answer, this isn't meant to be an answer, but a clarification. User#11440 120 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

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

1game.Players.PlayerAdded:connect(function(plr)
2end)

That is what you can use!

Log in to vote
1
Answered by
Avectus 120
8 years ago
1player = 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:

1player = 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

Answer this question