Right now, my respawning script kind of works but it always returns an error that the script's parent (script is inside the NPC) is a nil value(line 14), which is fine since I do not want it to respawn. Anyway, even if the script's parent is not nil, it theoretically should not run anyway since the NPC's humanoid health should not be less than 1.
local once = true local hum = script.Parent:FindFirstChild("Enemy") local expgiven = script.Parent:WaitForChild("EXPGiven") local awardee = script.Parent:WaitForChild("PlayerLastHit") while once == true do hum.Health = hum.MaxHealth latest = script.Parent:Clone() hum.BreakJointsOnDeath = false once = false end while wait(1.5) do if(script.Parent.Enemy.Health < 1) then local playerxp = game.Players[awardee.Value].OtherStats:WaitForChild("EXP") local playercash = game.Players[awardee.Value].OtherStats:WaitForChild("Cash") playerxp.Value = playerxp.Value + expgiven.Value playercash.Value = playercash.Value + math.random(1,10) wait(10) --respawn duration after death local respawn = latest:Clone() ---so we can clone the not dead dummy here respawn.Parent = game.Workspace respawn:makeJoints() respawn.Enemy.Health = respawn.Enemy.MaxHealth script.Parent:Destroy() end end
However here is the issue, if I change the if statement section to this instead:
while wait(1.5) do if(hum.Health < 1) then local playerxp = game.Players[awardee.Value].OtherStats:WaitForChild("EXP") local playercash = game.Players[awardee.Value].OtherStats:WaitForChild("Cash") playerxp.Value = playerxp.Value + expgiven.Value playercash.Value = playercash.Value + math.random(1,10) wait(10) --respawn duration after death local respawn = latest:Clone() ---so we can clone the not dead dummy here respawn.Parent = game.Workspace respawn:makeJoints() respawn.Enemy.Health = respawn.Enemy.MaxHealth script.Parent:Destroy() end end
It will not return the error anymore but after 10 seconds, it would just respawn, meaning it simply ignored my if statement. Somehow, it detects that the NPC's health is lower than 1 straight after it respawned. I have tried other methods too like using the HealthChanged event but that event simply will not fire. I have set prints after it but it simply will not print the humanoid's health even after damaging it.
Please help me solve this, maybe it's just something really dumb or I got the order wrong.