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.

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'.

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,

-- 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)
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

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

That is what you can use!

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

Answer this question