How can I wait for a child in this function?

Hey everyone!

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!

~innit_2winnit

1 Like

Is this function being run on startup on the server?

It’s on a serverscript in serverscriptservice. It’s one of the functions in my gameloop.

Is the function running after the players have joined

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?

What do you mean wait until everything?

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.

Why not just use FireAllClients?

1 Like

that is a great idea! i’ll give that a try tomorrow and get back to you :slight_smile:

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!

1 Like

Yep! Adding a Wait() function is what I needed! Thanks for the help regardless, sorry for being so clueless! :sweat_smile:

Use task.wait please

Rawblacks

It’s not the correct way to do it. You shouldn’t change a player’s GUI via a server script. Instead, you should research remote events.

What do you mean by “correct” way?

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.

Alright. I used task.wait()

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!) :slight_smile:

All the best,

~innit_2winnit

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.