I have this function that allows a textlabel to have it’s text changed. However, it works rather haphazardly. I think the function is trying to manipulate a screenGUI before it gets replicated to the player. Here’s the function:
function displaytext(text)
for _,v in pairs(game.Players:GetChildren()) do
v.PlayerGui.ScreenGui.TextLabel.Text = text
end
end
Is there a way I can wait until everything is loaded? I’ve tried WaitForChild() but to no avail.
Thanks in advance!
I’m slightly confused, you’re saying WaitForChild doesn’t work? Are you sure the issue is related to the child not existing? Are there any errors that are printing out?
Essentially, I am trying to allow a serverscript to directly change the text in a textlabel in starterGUI. However, I have now found out that I need to do this for every local player as opposed to globally.
The issue is that I do get an error, it tells me that startergui is not a correct child. But this only happens sometimes, usually to the second player in the game when they load later than the first.
This is what makes me think that I need to WaitForChild() or something similar to give the second player time to load in.
Well uhh sorry to break it to you but that’s in pretty sure impossible. waitforchild usually waits a bit after the instance has been replicated so there’s like no chance. Also why not just change it in player gui???
My function changes the text in playergui but I will sometimes get an error
I also tried FireAllClients() but I’m not sure how to get it to work with my current function setup.
UPDATE:
I think I might have solved it by adding a Wait(10) to the top of my gameloop. Will do some further testing today/tomorrow and if all is well then I will mark this as solution!
I know it probably isn’t the most efficient way to do it (and probably a very strange way to do it) but it does work! and so far I’ve seen little to no performance impact.
Adding a wait(10) is no solution to the problem. It merely mask the fact that the method is flawed. If a player has a slow ping/processor, or their WiFi connection drops significantly they may need more than 10 seconds for this to work. And everyone else with super fast broadband connections are being delayed unnecessarily by 10 seconds in the arbitrary wait(). It may seem to work, even a thousand times before it breaks, but this will break sooner or later.
Thank you for your feedback. It looks as if I haven’t been considering all internet connection methods, and for that, I am sorry.
I would like to add that this only counts for the first player who joins the game; the task.wait line is the first line in the script where variables are defined, and outside of the gameloop.
Thank you for replying, and thanks to @bloodbonniekingnoob1 for introducing me to task.wait() (super useful and very versatile I will make use of this a LOT!)