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

[EDIT] Loop working fine in test mode, but not online?

I have this script where it would loop. It works in test mode, however online, it throws an error saying PlayerGui is not a valid member of Script.

Would anyone be able to help? Thanks

for _,a in pairs (game.Players:GetChildren()) do
        if not (a.PlayerGui:findFirstChild("Main")) then

-EDIT- Here is the full script that I am using this in. It prints the line 3 but not the line 7 which means it's not passing. This is also the edited code with the loop searching for PlayerGui, before on line 6, it would give an error saying PlayerGui is not a valid member of Script. So basically, it's not finding PlayerGui in the player.

wait(5)
while wait(10) do
	print("easyJet Allocated Seating: Checking players. /Script: CheckPlayer/")
	for _,a in pairs (game.Players:GetChildren()) do
		repeat wait() until a:findFirstChild("PlayerGui")
		if not (a.PlayerGui:findFirstChild("Main")) then
			print("easyJet Allocated Seating: Given booking GUI to "..a.Name..". /Script: CheckPlayer/")
			local bookedvalue = script.Parent.Booked
   			local newvalue = bookedvalue:clone()
    		newvalue.Parent = a
			local newscript = script.Parent.AllocatedSeating:Clone()
			newscript.Parent = a.PlayerGui
			newscript.Disabled = false
			local gui = script.Parent.Main
			local newgui = gui:clone()
			for _,b in pairs (game.Workspace:GetChildren()) do
				if b:IsA("Model") then
					local asvalue = b:findFirstChild("AllocatedSeating")
					if asvalue then
						if b.Name == "Model" then
							plane = b:findFirstChild("easyJetAirbus A319")
						else
							plane = b
						end
					end
				end
			end
			newgui.Parent = a.PlayerGui
			newgui.Adornee = plane.PlaneKit.Plane.AllocatedSeatingBoard
			newgui.Enabled = true
		end
	end
end

3 comments

0

I would suggest using :GetPlayers() to be on the safe side in case there is something stored in players that isn't a player and that is erroring it, you may also want to add a check to see if "a" actually has a playergui then run that

0

Is it doing this at a specific time or whenever someone enters the game?

0

When I insert the model into the game, this script runs.

2 answers

for _,a in pairs (game.Players:GetPlayers()) do
		repeat wait() until a:findFirstChild("PlayerGui") -- Repeats waiting until a PlayerGui is found.
		if a.PlayerGui:findFirstChild("Main") then
		end
end

8 comments

0

Okay, if it takes a while to load, how can I make it if PlayerGui is not there yet, it would keep looping that second if until it loads?

0

Okay, let me edit this.

0

done.

0

Thank you, I'll try this now.

0

But this is getting all players it's not waiting for them to load :3

0

instead of having it keep looping until there is one, you can have it wait for one inside the player. You can surround this in a while true do if you want it to keep checking, but we do not know what he would use this for, and we dont want to mess up the script he is making with that while true do.

0

Let me edit this with the full script and explain it more.

0

Edited with the full script, any help is appreciated.

The trick is going to be WaitForChild(). This function yields until the given object is in, so you can write the script like so:

for _,a in pairs (game.Players:GetPlayers()) do
        a:WaitForChild("PlayerGui") -- Waits until a PlayerGui is found.
        if a.PlayerGui:FindFirstChild("Main") then
		print("True")
	else
		print("False")
        end
end

Be careful though, if the object doesn't show up or you spelled it wrong, it will perpetually yield, never moving on.

I also left in the FindFirstChild part because if you are not using CharacterAutoLoad, the objects in StarterGui won't be automatically placed into the PlayerGui until they spawn the first time (which could have led to the perpetual yield).

1 comment

0

That's not the issue. The script is saying on my line 6 that PlayerGui is not a valid member of Script, but I'm not telling it to search in Script.

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