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.
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)
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)
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.
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.