Player stays on Playerlist after they have left the server

Hello there,

I am having this issue for my leaderboard, where when someone joins, it shows the amount of people playing and then it shows their profile picture and group rank. However when they leave the server, they still appear on the leaderboard. Is there any way to fix that?

What's occuring


Code
local groups = {
   
    { id = 1245097, name = "WESSEX" },
    { id = 3411267, name = "SVEALAND" },
    { id = 3240949, name = "MERCIA" },
    { id = 1246558, name = "NORTHUMBRIA" },
    { id = 3247990, name = "DANELAW" },
    { id = 3493953, name = "EAST ANGLIA" },
    { id = 3418930, name = "DANES" },
    { id = 3416823, name = "NORSE" },
    { id = 3412261, name = "GOTALAND" },
    { id = 3181532, name = "NORRLAND" },
    { id = 4121922, name = "DUBLIN" },
    { id = 1124324, name = "ICELAND" },
    { id = 3458210, name = "KIEVAN RUS" },
    { id = 4121912, name = "THE ISLES" },
    { id = 4121933, name = "THE POWYS" },
    { id = 3778509, name = "DYFED" },
    { id = 3493930, name = "CORNWALLUM" },
    { id = 3269061, name = "ALT CLUT" },
    { id = 3265487, name = "GWYNEDD" },
    { id = 3316962, name = "PICTLAND" },
    { id = 3172503, name = "MUNSTER" },
    { id = 3490619, name = "GLYWYSING" },
    { id = 3406180, name = "ULSTER" },
    { id = 3507435, name = "MEATH" },
    { id = 3406391, name = "CONNACHT" },
    { id = 3413132, name = "LEINSTER" },
    { id = 3414659, name = "BRITTANY" },
    { id = 3779588, name = "MIDDLE FRANKIA" },
    { id = 3419708, name = "WEST FRANKIA" },
    { id = 3421863, name = "EAST FRANKIA" },
    { id = 3357473, name = "MERCENARIES" },
    { id = 3422678, name = "THE CHURCH" },
    { id = 3128836, name = "PAGAN CHURCH" },
   
}
 
local ld = script.Parent:WaitForChild("Leaderboard")
local list = ld:WaitForChild("List")
--local rank = ld:WaitForChild("Player"):WaitForChild("PlrRank")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local enterfading = false
local leavefading = false
local gui = game:GetService("StarterGui")
gui:SetCoreGuiEnabled("PlayerList",false)
 
local function update()
    -- Get rid of the existing tags
    for e,d in pairs(list:GetChildren()) do
        d:remove()
    end
 
    for i,v in pairs(game.Players:GetChildren()) do
        if v ~= nil then
            for e,d in pairs(list:GetChildren()) do
                d.Position = d.Position + UDim2.new(0,0,0,d.Size.Y.Offset)
            end
 
            local tag = ld:WaitForChild("Player"):Clone()
            local plrName = tag:WaitForChild("PlrName")
            tag.Parent = list
 
            -- Make the name in uppercase letters for charity
            plrName.Text = string.upper(v.Name)
 
            -- Set Player's Rank
            tag.PlrRank.Text = string.upper(v:GetRoleInGroup(2965628)) -- Change to your group ID
 
            -- Set Player's Group
           for _, group in pairs(groups) do
            if v:IsInGroup(group.id) then
                tag.GrpRank.Text = group.name
                break
            else
                tag.GrpRank.Text = "NO FACTION"
            end
        end
 	 tag.GrpRank.Visible = true
 
            -- Set Player's Thumbnail
            local content, bool = game.Players:GetUserThumbnailAsync(v.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size150x150)
            tag.ImageLabel.Image = content
            tag.Visible = true
 
            tag.MouseEnter:connect(function()
                repeat wait() until leavefading == false
                enterfading = true
                repeat
                    tag.Brightness.BackgroundTransparency = tag.Brightness.BackgroundTransparency - 0.02
                    wait()
                until tag.Brightness.BackgroundTransparency <= 0.8
                tag:FindFirstChild("Brightness").BackgroundTransparency = 0.8
                wait(0.2)
                enterfading = false
            end)
 
            tag.MouseLeave:connect(function()
                repeat wait() until enterfading == false
                leavefading = true
                repeat
                    tag.Brightness.BackgroundTransparency = tag.Brightness.BackgroundTransparency + 0.02
                    wait()
                until tag.Brightness.BackgroundTransparency >= 1
                tag.Brightness.BackgroundTransparency = 1
                wait(0.2)
                leavefading = false
            end)
        end
    end
 
    list.CanvasSize = UDim2.new(0,0,0,#list:GetChildren()*45) -- Make sure we update the size of the list
end
 
game.Players.PlayerAdded:connect(update)
 
mouse.KeyDown:connect(function(key)
    if string.lower(key) == "l" then
        if ld.Visible then
            game.Lighting.Blur.Size = 0
            script.Parent:FindFirstChild("Open"):Play()
            ld.Visible = not ld.Visible
        elseif not ld.Visible then
            ld.Visible = true
            game.Lighting.Blur.Size = 15
            script.Parent:FindFirstChild("Close"):Play()
        end
    end
end)
 
update()
1 Like

Well you should add a game.Players.PlayerRemoving event listener in there, and remove the removing player from the list when the event is fired.

1 Like

Would I need to add any code other than the game.Players.PlayerRemoving and the event

Inside Update(), you should save the new “tag” inside of a table like so

local listed = {}

local function Update()
    -- your loop, where you create the "tag"
    listed[player.Name] = tag -- Save this tag inside the listed table, so we can remove it later
end

local function Remove(player)
    if listed[player.Name] then -- easily check and delete the listed 'tag'
        listed[player.Name]:Destroy()
        listed[player.Name] = nil
    end
end
game.Players.PlayerRemoving:Connect(Remove)
1 Like

Alternatively, you could just connect the update function to the PlayerRemoving event.

1 Like

I tried the solution you gave me but the error still occurs. Any other suggestions?

Edit: I found out if you reset the changes happen. Is there a way to make it happen live without having to reset?

Players.PlayerRemoving fires before the player is gone (or nil), it’s giving you a heads up so you can do things that still need the player reference, like update their datastores. If you loop over Players:GetPlayers() in a function called from PlayerRemoving, they’ll still be there. You can’t just hook PlayerRemoving to update(), unless update() becomes update(removingPlayer), and you use the reference that the PlayerRemoving event provides to skip adding that player to the leaderboard when you rebuild it.

4 Likes

sorry dude, I forgot to tell you that you needed a PlayerRemoving function. D:

1 Like