✓ Accepted answer
What your code snippet does is that it checks every (approx) 1/30th of a second for the Animator to be a descendant of DataModel (commonly referred to as game, due to that being the global keyword in scripts).
My solution would be to add an exception for when the character dies (note: I have not defined character. It's not possible for me to accurately get it for you based on the code provided. You will have to do that yourself):
while not Animator:IsDescendantOf(game) do
if character and character:FindFirstChild("Humanoid") then
if (character.Humanoid.Health < 1) then
break
end
end
wait()
end
As a bonus, you could make it check every 0.5 seconds, and drastically improve performance. There's no need to check 30 times a second, that's just a waste. Learn to code cleanly & efficiently!
Example code:
Instead of:
while true do
wait()
end
You could try something like:
while true do
wait(0.5)
end
In most cases the time difference wont matter, trust me. It is very uncommon to need a loop running that many times a second!