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

Can yall help me with this Candy Giver Script i got so far?

i am making a trick or treat game and i need help with a trick or treat GUI her is the script i made

script.Parent.MouseButton1Click:Connect(function(player)
	script.Parent.Parent.TextLabel.Text = "Have a Nice Night!"
	wait(1)
	game.Players.LocalPlayer.ScreenGui.CandyFrame.Visible = false
end)

waht i am really having trouble with is the fourth line it says: 22:27:58.272 - ScreenGui is not a valid member of Player

4 comments

0

I just realised its the 30th i have been working on this since the 10th this month

0

You know what? im going to keep working on this and release the update next halloween

0

AND i got the houses done like 4 days ago

0

all GUI elements are descendants of player.playerGui, and not the player itself

1 answer

✓ Accepted answer

So basically the GUI's don't load in as fast as the player does. Easy fix.

We use the WaitForChild() Method to wait for a child. WaitForChild() "yields" (waits) the script until it has found a child. Unlike FindFirstChild(), it returns a true or false boolean, making it effective for if statement procedures.

WaitForChild's purpose is to not let parents of the children load too fast making errors.

There is also another problem. You are referencing the GUI under the player itself. There is about two solutions to fix that and I'll show it.

Your script fixed by WaitForChild() and the GUI thing would be:

script.Parent.MouseButton1Click:Connect(function(player)
	    script.Parent.Parent:WaitForChild("TextLabel").Text = "Have a Nice Night!"
	    wait(1)
	    game.Players.LocalPlayer.PlayerGui:WaitForChild("ScreenGui").CandyFrame.Visible = false
	end)

You can ask clarification any time.

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