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

Getting players from non local script help?

Asked by
NotSoNorm 777 Moderation Voter
9 years ago

When I run this, I constantly get an error saying: 'MainGui is not a valid member of playergui'

            for _, Player in pairs(game.Players:GetChildren()) do
        if Player:IsA("Player") then
            Player.PlayerGui.MainGui.TimeFrame.Timer.Text = "Intermission: "..timewait
        end
    end
1
I think you need to put an :WaitForChild --> player.PlayerGui:WaitForChild("MainGui") if the code don't run its because MainGui is nil XToonLinkX123 580 — 9y

2 answers

Log in to vote
2
Answered by 9 years ago

Sometimes, some objects don't load straight away and thus cause an error because they're not ready to be accessed yet.

To solve this issue, you just need to use WaitForChild, which will yield (wait) the current script thread until the child you're looking for is existent.

for _, Player in pairs(game.Players:GetChildren()) do
    if Player:IsA("Player") then
        Player.PlayerGui:WaitForChild("MainGui").TimeFrame.Timer.Text = "Intermission: "..timewait
    end
end

If your code doesn't work at all, make sure the spelling and punctuation of MainGui is correct, anything which could cause MainGui to be nil.

I hope my answer helped you. If it did, be sure to accept it.

Ad
Log in to vote
0
Answered by
drahsid5 250 Moderation Voter
9 years ago

That would work perfectly if MainGui was a valid member. By the way, probably hasn't initialized MainGui yet, wait until the player has completely initialized before trying to redefine variables.

Also, if there is an intermission, it would be more easily controlled like this:

inttime = 30
timeDef = 60
time = 0
int = Instance.new("BoolValue",game.Workspace)
int.Name = "Intermission"
int.Value = false --Not needed

function timer()
time = timeDef --reset the time
repeat time = (time - 1) wait(1) until time <= 0 --Continually lower the time until it is 0, also note the tick() function
int.Value = true --Intermission is true
end

function intermission()
time = inttime --Reset the time to the intermission time
repeat time = (time - 1) wait(1) until time <= 0 --Lower until the intermission should be over
int.Value = false --It is over
end

timer()

int.Changed:connect(function() --When it changes
if int.Value == true then --And if it's true
intermission() --Run the intermission function
else timer() --else run the game timer
end
end)
timer() --start the loop

and in a local script inside the GUI's text

repeat wait() until game.Workspace:FindFirstChild("Intermission") --wait until the bool exsists
while wait(1/15) do --loop
if game.Workspace.Intermission.Value == true then --when the intermission starts
script.Parent.Text = "Intermission" --change the text
else script.Parent.Text = "Not Intermission" --if it's not at the intermission, the text is different
end
end

Hope this helped!

Answer this question