I'm doing a test script to play around with for loops & looping through available players. The script is simple!
print("Script initiated.") function Run() for i, v in ipairs(game.Players:GetPlayers()) do -- Get available players! wait(12); -- In case contents inside PlayerGui has not been loaded! print(v); -- Prints the current iteration, player's name (current iteration). print(game.Players[v.Name].PlayerGui.testFind); -- Simple, there's a ScreenGui! end end game.Players.PlayerAdded:connect(Run)
Very simple. Yet doesn't work. Why!?
It works in studio and this is what it outputs:
Script initiated. Player1 testFind
However if you try it online, there's suddenly an error!
Script initiated. Player... added Arithmeticity testFind is not a valid member of PlayerGui
The script is placed in ServerScriptStorage.
Here's the model if you want to take a look at it and see why: click to see the model.
Am I missing something? Please help me as this is confusing me. It should be e-z-p-z.
Update(s):
Just got helped by ScriptGuider. He says that server cannot access the contents of PlayerGui so that may be a possibility. I'm now trying to work around it instead of doing this method. See how it goes and all, so far so good. Thanks to ScriptGuider! If ScriptGuider reads this, maybe post a reply so I can accept your answer perhaps.
Edited the script yet again to include WaitForChild. Here it is:
print("Script initiated.") function Run() for i, v in ipairs(game.Players:GetPlayers()) do print(v); game.Players[v.Name].PlayerGui:WaitForChild("testFind"); print(game.Players[v.Name].PlayerGui.testFind); end end game.Players.PlayerAdded:connect(Run)
Output has been changed, it's now this:
Script initiated. Arithmeticity. Player ... added Infinite yield possible on 'Players.Arithmeticity.PlayerGui:WaitForChild("testFind")' Stack Begin Script 'ServerScriptService.testIterations', Line 5 Stack End
I have also updated the model so if anyone can please take it and take a look in their own game, that'll be really helpful!
Past edit 2/1/2017:
print("Script initiated.") function Run() for i, v in ipairs(game.Players:GetPlayers()) do print(v); repeat wait() until game.Players[v.Name].PlayerGui.testFind; print(game.Players[v.Name].PlayerGui.testFind); end end game.Players.PlayerAdded:connect(Run)
The output is pretty much the same results. Why is this happening!?
When dealing with the local player, you're going to need WaitForChild. The method will not permit the script to continue until the specified object is found, which in your case, is a GUI. I recommend you get familiar with the method, if you're going to be dealing with he client a lot.
Instead of using wait
, try using a repeat until
system. Like so:
repeat wait() until game.Players[v.Name].PlayerGui.testFind
That could solve your issue, also, semicolons are not necessary in Lua code... just to let you know.