So I have a boss fight system in my game which seems to work fine, except when a player leaves while fighting a boss. When this happens, the boss fight script errors and does not reset the IsPlayerFightingBoss value to false, making it so that nobody else can fight a boss until the server resets.
The problem is, I don’t know how to tell if a player leaves. Here is my code that happens while the boss is being fought:
local playerIsntNil = true
while playerIsntNil and player.Character:FindFirstChildOfClass("Humanoid").Health > 0 and boss:FindFirstChildOfClass("Humanoid").Health > 0 do
wait()
if player == nil then
playerIsntNil = false
break
end
end
The reason it’s a while loop is because I am waiting for the boss to end. I have tried if not player then and it doesn’t work. I have tried adding a print statement, and have determined that it is restarting the loop before detecting if the player is nil, meaning that player is nil and :FindFirstChildOfClass will error, which is exactly what is happening.
Basically, I am looking for a way to find if player == nil, and I cannot figure it out, and I cannot use Players.PlayerLeaving the way my code is set up.
EDIT: Now the boss is the problem, managed to add a players.PlayerRemoving event. Returning the same error
