Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this not working?

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

✓ Accepted answer

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!

3 comments

1

YOU COPIED MY ANSWER!

1

Come on man... not cool

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.