PlayerAdded Clone

Hey devs, thanks for stopping by. :slight_smile:

Basically, once a player joins my game I need them to be added to a UI for moderators.
I’m using a UIListLayout, and whenever I tell it to clone, It simply doesn’t.

Server Side: (FE)

local events = game:GetService("ReplicatedStorage"):WaitForChild('AdminRemotes'):WaitForChild('UpdateEvents')

game.Players.PlayerAdded:Connect(function(plr)
	events:WaitForChild('UpdatePlayers'):FireAllClients(plr)
end)

My LocalScript:

local events = game:GetService("ReplicatedStorage"):WaitForChild('AdminRemotes'):WaitForChild('UpdateEvents')

events:WaitForChild('UpdatePlayers').OnClientEvent:Connect(function(plr)
		script.Parent.ScrollingFrame.Player:Clone()
	end
end)

Thanks! :wink: (The “plr” argument is used in the script, but I’ve edited it to fit the post.)

1 Like

Calling :Clone() will make another copy of it, but that’s all it does - it won’t automatically parent it to your frame or anything. I think in this case you’d need:

  1. Make the clone (what you have)
  2. Replace the text inside the GUI with the player name
  3. Parent the GUI back into your scrolling frame

So something like this:

local events = game:GetService("ReplicatedStorage"):WaitForChild('AdminRemotes'):WaitForChild('UpdateEvents')

events:WaitForChild('UpdatePlayers').OnClientEvent:Connect(function(plr)
		local clone = script.Parent.ScrollingFrame.Player:Clone()
                -- Set the text
                clone.Parent = script.Parent.ScrollingFrame
	end
end)
3 Likes

Thank you! This will help me with this project, along with many more! :smiley:

2 Likes

Why do you even need a server script for this? You can literally get rid of the server-side and put this all on the client. Don’t waste server memory, even if just a little, for something that you can do in a better manner. You’re pretty much reinventing the wheel here anyway.

-- All in a LocalScript

local Players = game:GetService("Players")

local function AddPlayerToList(Player)
    -- You also didn't handle your entry clone in your original implementation
    local newEntry = script.Parent.ScrollingFrame.Player:Clone()
    newEntry.something = other
    newEntry.Parent = script.Parent.ScrollingFrame
end

Players.PlayerAdded:Connect(AddPlayerToList)

for _, Player in pairs(Players:GetPlayers()) do
    AddPlayerToList(Player)
end

Players.PlayerRemoving:Connect(function (Player)
    -- Delete the entry
end)

There’s no need for either a server codebase or remote in this scenario.

As for the issue at hand, remember that methods don’t implicitly do things for you. When you clone something, all it does is make a clone and that’s it. You need to explicitly handle this clone yourself. Given that you want to do more with it, assign it to a variable, set its properties then parent it over. I’ve handled that in the code sample above.

2 Likes