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:

01local function interm(player)
02    spawn(function()
03        if player.inInterm.Value == false then
04            player.inInterm.Value = true
05            table.insert(playerInterm, player)
06        end
07    end)
08end
09 
10script.Parent.Touched:Connect(function(hit)
11    pcall(function()
12        if game.Players:GetPlayerFromCharacter(hit.Parent) then
13            if hit.Parent:FindFirstChild('inInterm') then              
14                interm(p.Parent)
15 
View all 22 lines...

Script the gives error:

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

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:

1while true do
2    wait(1)
3    for I=1,#playerInterm
4        local Character=playerInterm[I]
5        if not game:GetService'Players':GetPlayerFromCharacter(Character) then
6            table.remove(playerInterm,I)
7        end
8    end
9end
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.

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

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