Server script can't find a players PlayerGui

I have a part of a server script that is meant to go into a players PlayerGui when they join, delete a certain GUI. It is then meant get a random player from the server, and clone their gui into that players gui but it can’t find PlayerGui in the player. Just get the infinite yield error for WaitForChild. Anyone know why?

Players.PlayerAdded:Connect(function(player)
	player.PlayerGui:WaitForChild("Menu"):Destroy()
	local Copy = randomPlayer:WaitForChild("PlayerGui"):WaitForChild("Menu"):Clone()
	Copy.Parent = player:WaitForChild("PlayerGui")
end)

Maybe try using a for I, v in pairs do loop

Try using WaitForChild on the PlayerGui, Not sure if that’ll make it work. The PlayerGui usually takes some time to Replicate, So i guess waiting for it might be the solution.

player:WaitForChild("PlayerGui"):WaitForChild("Menu"):Destroy()

I’ve tried that as well, still doesn’t work

It’s not an error, it’s a warning. It’s saying the thing you are looking for might not exist.

Anyway, why are you using the server to do this? Just have a localscript do it, I’m pretty sure handling UI on the server is bad practice.

I have a team creation/selection ui, not really sure the best way to do it, tried 3 other methods and what I’m doing is the only one which allows everything to work, the server script isn’t actually changing the ui, it’s just doing commanding what happens and sending remote events to the local script that changes the ui. I thought that maybe it was saying that because maybe the PlayerGui just hadn’t been loaded by the time it checks so I delayed it and still got it.

The code you sent appears to just put things into the player’s PlayerGui, and there’s no reference of remotes in the 5 lines of code you gave us. Why isn’t the UI in the PlayerGui in the first place?

That’s just the function that is supposed to update the new players gui, didn’t really think it was necessary to include the entire script seeing that the rest of the script doesn’t interact with that function at all.

Why don’t you just have the UI in the player’s PlayerGui from the start but just have it disabled?

Like the original menu? Because it’s edited by other players as they create new teams directly in their PlayerGui.

Just store whatever edits players have made in ReplicatedStorage, and apply them whenever someone gets picked. Or, have a localscript clone the UI from ReplicatedStorage and put it under the LocalPlayer when the player gets picked.

I did think of something similar, just thought this way would be easier but seeing as its not working I’ll give it a try.

At what line does it show the warning for infinite yielding? Also why don’t you use RemoteEvents, that’s way better. Is there some specific reason?