Cloned TextButton Doesn't work

Hi,

I’m making a system where you can access a Frame of all the players in the game and there is a TextButton for every player on the server. You can then click the buttons and do things to the selected player.

However, I’m having problems after refreshing the list. When a player leaves or enters I want the frame to refresh to the number of players currently on the server.
I used the following code to first making the list when a players joins:

	for i,v in pairs(game.Players:GetPlayers()) do
		local newTemplate = template:Clone()
		newTemplate.Name = v.Name
		newTemplate.Text = v.Name
		newTemplate.BackgroundTransparency = 0
		newTemplate.Parent = playerFolder
	end

This does its Job and makes Text Buttons for every player in the server.
To refresh I used this code:

        for i,v in pairs(playerFolder:GetChildren()) do
		if v:IsA("TextButton") then
			v:Destroy()
		end
	end
	for i,v in pairs(game.Players:GetPlayers()) do
		local newTemplate = template:Clone()
		newTemplate.Name = v.Name
		newTemplate.Text = v.Name
		newTemplate.BackgroundTransparency = 0
		newTemplate.Parent = playerFolder
	end

This does the job and refreshes the list and makes exactly the number of text boxes as the number of people on the server.
But the TextButtons stop working… They worked when I first created them when the player first joined the game. But, suddenly when refreshing them they stop working.

I’ve tried to do other things but nothing worked
Help would be appreciated
Thanks in Advance,

2 Likes

Okay so what you need to do is to make three different functions. First function for start up, second for when a new player joins and the third one for when a player leaves.

So this how is how you’ll do the first function:

local function GetPlayers()
    for i, player in ipairs(game.Players:GetPlayers()) do
        local newTemplate = template:Clone()
        newTemplate.Name = player.Name
        newTemplate.Text = player.Name
        newTemplate.BackgroundTransparency = 0
        newTemplate.Parent = playerFolder
    end
end

Now for the second function that will trigger when a new player join the game!

local function PlayerAdded(plr)
   local newTemplate = template:Clone()
   newTemplate.Name = plr.Name
   newTemplate.Text = plr.Name
   newTemplate.BackgroundTransparency = 0
   newTemplate.Parent = playerFolder
end

And now for the third function that will remove the template that correspond to the removing player:

local function PlayerRemoving(plr)
   for i, instance in ipairs(playerFolder:GetChildren()) do
       if instance.Name == plr.Name then
           instance:Destroy()
       end
   end
end

Now all you got to do is to right put the line of code that will trigger the function at the end of the script!

local function GetPlayers()
    for i, player in ipairs(game.Players:GetPlayers()) do
        local newTemplate = template:Clone()
        newTemplate.Name = player.Name
        newTemplate.Text = player.Name
        newTemplate.BackgroundTransparency = 0
        newTemplate.Parent = playerFolder
    end
end

local function PlayerAdded(plr)
   local newTemplate = template:Clone()
   newTemplate.Name = plr.Name
   newTemplate.Text = plr.Name
   newTemplate.BackgroundTransparency = 0
   newTemplate.Parent = playerFolder
end

local function PlayerRemoving(plr)
   for i, instance in ipairs(playerFolder:GetChildren()) do
       if instance.Name == plr.Name then
           instance:Destroy()
       end
   end
end

-- This is where the Functions get triggered
GetPlayers()
game.Players.PlayerAdded:Connect(PlayerAdded)
game.Players.PlayerRemoving:Connect(PlayerRemoving)

Hope this was helpful!

Hi, thanks for this detailed reply.
I tried this but it doesn’t seem to work.

When you using for loop use clone mouse event like this

for i,v in pairs(game.Players:GetPlayers ())do
      local newTemplate = template:Clone()
      newTemplate.MouseButton1Down:connect(function()
           ....
      end)
end 

And if you went to check player name try that

game.Players.PlayerRemoving:Connect(function(plr)
    If playerFolder:FindFirstChild(plr.Name) then 
        playerFolder[plr.Name]:Destroy()
    end
end)

I hope this helped you

Fixed it bymyself. (characters)

1 Like