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