Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
traigla 75
8 years ago

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
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 Ryzox 220 — 8y
0
Is it doing this at a specific time or whenever someone enters the game? Scubadoo2 115 — 8y
0
When I insert the model into the game, this script runs. traigla 75 — 8y

2 answers

Log in to vote
0
Answered by 8 years ago
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
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? traigla 75 — 8y
0
Okay, let me edit this. HungryJaffer 1246 — 8y
0
done. HungryJaffer 1246 — 8y
0
Thank you, I'll try this now. traigla 75 — 8y
View all comments (4 more)
0
But this is getting all players it's not waiting for them to load :3 Ryzox 220 — 8y
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. HungryJaffer 1246 — 8y
0
Let me edit this with the full script and explain it more. traigla 75 — 8y
0
Edited with the full script, any help is appreciated. traigla 75 — 8y
Ad
Log in to vote
0
Answered by
Scubadoo2 115
8 years ago

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

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. traigla 75 — 8y

Answer this question