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

Remove character from table when player leaves?

Asked by
U_srname 152
5 years ago

So I am trying so hard to make this work. Here's the situation:

When the player enters a sort of area, they are added to a table of players waiting for intermission. However, if a player gets added to the table, and then leaves the next time the game tries running a round the server will send an event to every player that is in that table. The problem is, I can't remove the player from the table when they leave, so the server tries finding that player (since they are still in the table) but can't because they don't actually exist.

Here is some code:

local function interm(player)
    spawn(function()
        if player.inInterm.Value == false then
            player.inInterm.Value = true
            table.insert(playerInterm, player)
        end
    end)
end

script.Parent.Touched:Connect(function(hit)
    pcall(function()
        if game.Players:GetPlayerFromCharacter(hit.Parent) then
            if hit.Parent:FindFirstChild('inInterm') then               
                interm(p.Parent)

                if #playerInterm ~= 0 then
                    script.Parent.start:Fire()
                end
            end
        end
    end)
end)

Script the gives error:

for i, v in pairs(playerInterm) do -- it tries firing this event to all players in the table. but some dont exist!!
    Blur:FireClient(game.Players[v.Name])
end

For your information, it's the character that gets added to the table. I don't want to change that because it is to complex because of all the other scripts that rely on this. I can't seem to find anyway to remove the character from the table when the player leaves...

2 answers

Log in to vote
0
Answered by
qChaos 86
5 years ago
Edited 5 years ago

You could try constantly looping through the table and using GetPlayerFromCharacter() and if it's nil it will remove it from the table:

while true do
    wait(1)
    for I=1,#playerInterm
        local Character=playerInterm[I]
        if not game:GetService'Players':GetPlayerFromCharacter(Character) then
            table.remove(playerInterm,I)
        end
    end
end
0
Wow U_srname 152 — 5y
0
this is very inefficient and it will still error from time to time. Elixcore 1337 — 5y
0
if you do have anything better then please share it with me U_srname 152 — 5y
Ad
Log in to vote
0
Answered by
Elixcore 1337 Moderation Voter
5 years ago
Edited 5 years ago

Simply connect to a PlayerRemoving event and remove the players that leave from the table.

game.Players.PlayerRemoving:Connect(function(player) -- fired when a player is about to leave.
    for i,v in next, playerInterm do -- loop through the table 
        if v.Name == player.Name then -- check if the player leaving is inside of the table
            table.remove(playerInterm, i) -- remove him
        end
    end
end)

if you want this to do the same thing when the character dies simply use the Humanoid.Died event.

0
Not trying to be rude or anything, but I specifically said that the it's not the player getting added to the table, it's the character. Maybe that might change your code... However, I guess this is pretty efficient U_srname 152 — 5y
0
you're right, here I'll change it then. Elixcore 1337 — 5y
0
Changing the if statement on line 3 to their names will make it work with either the character or the player. Elixcore 1337 — 5y

Answer this question