Is this necessary?

Hey guys! I’m attempting to find a gui in all player’s “PlayerGui”. But I have a question. Is this WaitForChild() necessary? I don’t want unnecessary stuff in my code, but I’m not sure if I need this WaitForChild() though.

function Functions.StartWave(WaveNumber, AU)
	if AU.Value then
		local Universe = AU.Value
		local Wave = Universe:WaitForChild("Wave "..WaveNumber, math.huge)
		for i, Player in ipairs(Players:GetPlayers()) do
			local PlayerGui = Player:WaitForChild("PlayerGui", math.huge)
			local Gui = PlayerGui:WaitForChild("Wave Gui", math.huge) -- Remove the waitforchild()? or keep it?
		end
	end
end
2 Likes

Using :WaitForChild() should always be used when getting the PlayerGui from a LocalScript.

1 Like

This is in a ServerScript. Is the :WaitForChild() still needed?

Depends when the function is run. Im guessing it probably runs when a player joins so its necessary cause the PlayerGui may not be rendered fast enough and before the function so it would just error and mess up the whole script

Edit: Also when it comes to little changes like that its best to leave it as is if it works

???

The PlayerGui object is only available on the client side, so this code should be run on the client, not the server.

1 Like

It is possible to access PlayerGui through serverScripts I’ve done it multiple times

2 Likes

its better to move this code to the client. If you need to open any gui for all player you can use Remote:FireAllClients()

PlayerGui is replicated locally for each client, hence in order to interact with a Player’s Gui I’d recommend using RemoteEvents.

1 Like

Well, I do have one more question. If this was put in a ModuleScript, would this :WaitForChild() still matter?

Yes because it still works the same, ModuleScripts are just used for organizatng large bits of code.

1 Like

Its always best practise using :WaitForChild() when it comes to scripts that run early on when a player joins

Okay, smart. Thanks everyone! I wish I could give you all the solution box for your help, but only one person can be chosen :frowning:
Okay, so, I made a poll. Who do you think had the best answer?

0 voters

Yes, that can be a difficult situation to be in :((

Glad to have helped out though!

2 Likes

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