PlayerAdded for playerlist not working

Im using this to clone a template into my UIListLayout constraint frame but it’s not cloning for some reason (I tested in server as well not only in solo play mode)

Code:

 print("Player Script Running")
game:GetService("Players").PlayerAdded:connect(function(player)
	print("Player Joined!")
    local template = script.Template:Clone()
template.Parent = script.Parent
    template.Name = player.Name
    template.TextLabel.Text = player.Name
end)


game:GetService("Players").PlayerRemoving:connect(function(player)
	print("Player Left!")
    local old = script.Parent:FindFirstChild(player.Name)
old:Destroy()
end)

Pic: image

The server prints “Player Script Running” but does not clone the template into my frame

pls help, Thank you

It has filtering enabled and its a localscript btw

Because your code is in a LocalScript, it will run after your player has been created. Your current code therefor won’t take you or the players already on the server into consideration.

You should instead do something like:

 print("Player Script Running")

local players = game:GetService("Players")

function addPlayer(player)
    print("Player Joined!")
    local template = script.Template:Clone()
    template.Parent = script.Parent
    template.Name = player.Name
    template.TextLabel.Text = player.Name
end

for _,player in pairs(players:GetPlayers())do
    addPlayer(player)
end

players.PlayerAdded:connect(addPlayer)

players.PlayerRemoving:connect(function(player)
    print("Player Left!")
    local old = script.Parent:FindFirstChild(player.Name)
    old:Destroy()
end)
4 Likes

It works! Thank you so much :grin: