How to parent a localscript to all players's playergui (Including New Players)

Hello! I like visualizers and serversided-scripting. Anyway, let’s get to the point.

I’m trying to parent this local script to EVERY player’s playergui. I know how to parent it to every current player in the server, but not to the new ones who just joined.

What do I do?

You put the script in the starter-gui.

just keep the script in StarterGui, contents of StarterGui are replicated for every client into their PlayerGui

No, I’m doing it with a module, have to use another method. (Sorry for not mentioning it before.)

You could use the PlayerAdded Event.

just do this (mobile sorry if any msitakes in code)

for i,v in pairs(game.Players:GetPlayers()) do
script.Gui:Clone().Parent = v.PlayerGui
end

Still not helping some how please use developer.roblox.com

1 Like

Does PlayerAdded include players in the server too?

No, it doesnt 30000 charsssssss

Tried it, worked but didn’t work for players that joined after.

Just fill out the single variable.

local ScriptToClone = -- Directory to the Local Script you'd like duplicated.

game:GetService("Players").PlayerAdded:Connect(function(player)
	local PlayerGui = player:WaitForChild("PlayerGui")
	if PlayerGui then
		ScriptToClone:Clone().Parent = PlayerGui
	end
end)
1 Like

Use my code used above and also do game.Players.Added:conn

1 Like

Does your code also include players in the current server?

No it doesnt 30 charrrrrrrssssss

I’ve tried to combine both of them but when I put them both in one normal script and parented that script to workspace with a module, it wouldn’t work. Maybe I did something wrong, I guess I can try again.

Here you go, this will add the local script to new players as well as all players in game that don’t currently have the Local Script within their PlayerGui.

local ScriptToClone = -- Directory to the Local Script you'd like duplicated.

game:GetService("Players").PlayerAdded:Connect(function(player)
	for i, player in pairs(game:GetService("Players"):GetPlayers()) do
	local PlayerGui = player:WaitForChild("PlayerGui")
		if PlayerGui then
			if not PlayerGui:FindFirstChild(ScriptToClone.Name) then
			ScriptToClone:Clone().Parent = PlayerGui
			end
		end
	end
end)
1 Like

Thank you so much!

30 Characters

1 Like

The PlayerAdded event fires for every player who joins the game’s server, what is your point?
unless you do it on the client, in which case the function bound to PlayerAdded won’t run for the current client.

@Cytheur

Verifying PlayerGui’s existence here is redundant because you already waited for it to exist, so it should.

Just for information: ipairs works faster (though by negligible difference) in these cases because of the Luau interpreter, not as slow as in vanilla lua.

1 Like