Leave/disconnect script

Is it possible to make this also work with people disconnecting and dying at the same time?

function died(playerName, humanoid)
if humanoid and humanoid.Health == 0 then
for i, v in pairs(survivors) do
if v == playerName then
table.remove(survivors, i)
end
end
end
end

Any help is appreciated.

Wrap code with triple ` to format. Makes things much more readable.

You’ll want to connect to this function whenever a player leaves the game and when a humanoid dies. This might help:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
      plr.CharacterAdded:Connect(function(chr)
             local humanoid = chr:WaitForChild("Humanoid")
             humanoid.Died:Connect(function()
                    died(plr)
             end) 
      end)
end)
Players.PlayerRemoving:Connect(died)

You will also need to adjust your function to take the player object as input

function died(plr)
     local ind = table.find(survivors, plr.Name)
     if ind then
          table.remove(survivors, ind)
     end
end
3 Likes

thanks for helping! This will help alot