I've created a function for my own purposes that updates text on every player's graphical interface. Only, it doesn't operate the for loop at all. I've plugged in prints to check, it passes right over the for loop without doing anything with it? Not quite sure why, I've never had this problem before.
local Players = game:GetService("Players"):GetChildren() function GUIUpdate (Type,Message) if Type == 'All' then print("ALL") --prints this for _,v in pairs(Players) do print(v.Name) -- doesn't print this print("for") -- or this v.PlayerGui.Status.FR.txtbox.Text = Message end end end GUIUpdate ('All',"Not enough players!")
Help is greatly appreciated.
This is happening because you get the children of the Players
service as soon as the script runs. The player likely hasn't finished loading in at this point. You can do a few things to avoid this, such as moving where Players
is defined:
function GUIUpdate (Type,Message) if Type == 'All' then print("ALL") --prints this local Players = game:GetService("Players"):GetPlayers(); --// Use GetPlayers instead of GetChildren here. for _,v in pairs(Players) do print(v.Name) -- doesn't print this print("for") -- or this v.PlayerGui.Status.FR.txtbox.Text = Message end end end GUIUpdate ('All',"Not enough players!")
While that fixes future issues with the function, it doesn't change the fact that it likely won't enter the loop. This is because you call GUIUpdate
immediately as well. You can wait for a player to join using thePlayerAdded
event of Players
:
local PlayersService = game:GetService("Players"); function GUIUpdate (Type,Message) if Type == 'All' then print("ALL") --prints this local Players = PlayersService:GetPlayers(); for _,v in pairs(Players) do print(v.Name) -- doesn't print this print("for") -- or this v.PlayerGui.Status.FR.txtbox.Text = Message end end end PlayersService.PlayerAdded:Wait() --// Wait for the event to occur. GUIUpdate ('All',"Not enough players!")
This should wait until a player has joined the game before updating. I assume you intend to use this differently and it's only in this form for testing; this function will only run once and won't continually check if there are enough players. You can check that using a while loop, among other methods.
You should also avoid changing players' GUIs in server scripts, use remote events for that.
Hope this helped.
Read more about GetPlayers.