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...
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
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.