#How would I make this for i,v loop keep track of all the current players?

13 messages · Page 1 of 1 (latest)

kind yarrow
#
for i,v in pairs(game.Players:GetPlayers()) do
    local button = script.Playerbutton:Clone()
    button.Text = v.Name
    button.Name = v.Name
    button.Parent = script.Parent.DampeningMF.ScrollingFrame
    
    button.MouseButton1Click:Connect(function()
        if workspace:FindFirstChild(button.Name) then
            button.MouseButton1Click:Connect(function()
                print(button.Name)
                local targetplr = Players[button.Name]
                print(targetplr.Name)
                ReplicatedStorage.Events:WaitForChild("Pressed"):FireServer(plr, targetplr)
            end)
        end
    end)
end
#

Basically what I'm trying to do is make a list with all the current players, but whenever someone joins after I do, his name does not appear in the list

mint axle
#

add the players name to a table every time it iterates through the loop

#
local table = {}
for i,v in pairs(game.Players:GetPlayers()) do
table.add(v)
    local button = script.Playerbutton:Clone()
    button.Text = v.Name
    button.Name = v.Name
    button.Parent = script.Parent.DampeningMF.ScrollingFrame
    
    button.MouseButton1Click:Connect(function()
        if workspace:FindFirstChild(button.Name) then
            button.MouseButton1Click:Connect(function()
                print(button.Name)
                local targetplr = Players[button.Name]
                print(targetplr.Name)
                ReplicatedStorage.Events:WaitForChild("Pressed"):FireServer(plr, targetplr)
            end)
        end
    end)
end
#

i think thats how you would do it

#

cant remember exactly

kind yarrow
#

Oh yeah that might work, thank you

dusky maple
#

Join*

#
game.Players.PlayerAdded:Connect(function(plr)
    local button = script.Playerbutton:Clone()
    button.Text = v.Name
    button.Name = v.Name
    button.Parent = script.Parent.DampeningMF.ScrollingFrame
    
    button.MouseButton1Click:Connect(function()
        if workspace:FindFirstChild(button.Name) then
            button.MouseButton1Click:Connect(function()
                print(button.Name)
                local targetplr = Players[button.Name]
                print(targetplr.Name)
                ReplicatedStorage.Events:WaitForChild("Pressed"):FireServer(plr, targetplr)
            end)
        end
    end)
end)

game.Players.PlayerRemoved:Connect(function(plr)
               script.Parent.DampeningMF.ScrollingFrame:FindFirstChild(plr.Name):Destroy()
end)
dusky maple
mint axle
#

at the top i also defined the table btw