Updating for i,v in pairs() problem

hi guys so it works and clones the buttons but the new cloned buttons dont work when new player joins
if i put it in a while wait loop it works but not good the buttons dont respond until 3 or 4 clicks. all help is greatly appreciated. thank you

game.Players.PlayerAdded:Connect(function(player)
 local clone = TextButton:Clone()
 clone.Visible = true
clone.Name = player.Name
end)
--[[ so when i joins it shows me buttons with all players names.
 when new player joins it clones that players button to me 
but the new MouseButton1Click:Connect doesnt work]]

for i,v2 in pairs(buttons:GetChildren()) do --{problem here. registers all exicting buttons but not the new buttons after script is executed }
if v2:IsA("TextButton") then

     v2.MouseButton1Click:Connect(function()
      if v2.TextColor3 == Color3.fromRGB(255, 255, 255)  then
     v2.TextColor3 = Color3.fromRGB(255, 0, 0)
      else v2.TextColor3 = Color3.fromRGB(255, 255, 255)
          
end    
end )
end
end

This is the wrong place, please move to #help-and-feedback:scripting-support.

1 Like

if v2:IsA("TextButton") then

oh yeah this script was a mock up lol thats not the issue though but thank u

I think that you need to set the parent to buttons.

local actParent = script.Parent -- put your parent here
game.Players.PlayerAdded:Connect(function(player)
   local clone = TextButton:Clone()
   clone.Visible = true
   clone.Name = player.Name
   clone.Parent = actParent
end)


local registredButtons = {}

function Update(buttons)
  for i,v2 in pairs(buttons:GetChildren()) do
     if v2:IsA("TextButton") then
         if not(table.find(registredButtons, v2)) then
            table.insert(registredButtons, v2)
            v2.MouseButton1Click:Connect(function()
               if v2.TextColor3 == Color3.fromRGB(255, 255, 255)  then
                    v2.TextColor3 = Color3.fromRGB(255, 0, 0)
               else
                  v2.TextColor3 = Color3.fromRGB(255, 255, 255)
               end    
            end)
         end
      end
   end
end

while wait() do
   Update(actParent)
end

3 Likes

You could use a table with the keys being the player names and that would be faster

1 Like

idk if he gonna add more buttons later

worked perfect thanks for your time mate

1 Like