Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to update a Playerlist?

Asked by 9 years ago

This is my first time making a player list, I did use the wiki for reference and it got me this far. However, I'm getting crumbled down with all of the logic and it's screwing me up. How would I clean this up? Am I even going in the correct direction in updating a player list?

The Player List Object and it's contents - http://prntscr.com/6xv9cg

The Playerlist Gui - http://prntscr.com/6xva5g

I'm trying to add a player tab to the player list when they join, by updating the Tab with Their Player name and level. And have it updated to all players. Similar to the ROBLOX Player list.

Could anyone help me clear out the unnecessary stuff and help fix it?

    function makePlayerTab(Player,PLAYER_FOR_TAB,Level,Last)
        local tab = game.ServerStorage.PlayerTab:Clone()
        tab.Parent = Player.PlayerGui.PlayerList.Holder:WaitForChild("PlayerList") -- something like this
        tab.PlayerName.Text = Player
        tab.Level = Level
        tab.Position = UDim2.new(0, 0,0.024, 0)
    return tab and spawn(function() if Last ~= nil then pcall(function() tab.Position = Last.Position + UDim2.new(0, 0,0.139, 0) end) end end)
    end

    function update(player)
        local Players = game.Players:GetChildren()
        local last;
        for i,v in next,Players do
            last = makePlayerTab(p,v,1337,last)
        end
    end

    game:GetService'Players'.PlayerAdded:connect(function(p)
        p.CharacterAdded:connect(function(c)
            update(p)
        end)
    end)
0
Have you tried the Changed Event? woodengop 1134 — 9y
0
I haven't made it so it changes when they upgrade a level but I know I'll need to, what is wrong with the current script I have..? IntellectualBeing 430 — 9y
3
If this is inside the PlayerGui, .PlayerAdded won't work. Try doing .ChildAdded(). Shrekerly 70 — 9y
1
PlayerRemoving is an event. Goulstem 8144 — 9y

1 answer

Log in to vote
0
Answered by 8 years ago

It looks like your algorithm is this:

  • Whenever a player joins the game, redo the GUI
  • For each player, add a line for them

It is easier than keeping track of which line is for which player and keeping them up to date, though the processing might cause lag if you keep it up to date this way.

local player = game.Players.LocalPlayer
while player == nil do wait() player = game.Players.LocalPlayer end
playerList = playerList:WaitForChild("PlayerGui"):WaitForChild("PlayerList"):WaitForChild("Holder"):WaitForChild("PlayerList")
function reset()
    playerList:ClearAllChildren()
end
function makePlayerTab(Player,Level,Last)
    local tab = game.ServerStorage.PlayerTab:Clone()
    tab.Parent = playerList
    tab.PlayerName.Text = Player.Name
    tab.Level.Text = Level
    tab.Position = Last and (Last.Position + UDim2.new(0, 0,0.139, 0)) or UDim2.new(0, 0,0.024, 0)
    --TODO listen to input events for tab.Invite and tab.Kick
    return tab
end

function update()
    reset()
    local Players = game.Players:GetChildren()
    local last;
    for i,v in next,Players do
        last = makePlayerTab(p,v,1337,last)
    end
end

game:GetService'Players'.ChildAdded:connect(function(p)
    p.CharacterAdded:connect(update)
end)
game.Players.ChildRemoved:connect(update)

Main changes:

  • Added reset and called it at the beginning of update
  • Added ChildRemoved and used ChildAdded instead of PlayerAdded (it will error if a non-player is ever added to Players for some reason)
  • Fixed a few bugs
  • Simplified parameters/argument passing
Ad

Answer this question