Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this script stop working when I die?

This is a serverscript inside Workspace. When I die, the particleemitter leaves, I know why, but I don't know how to fix it.

game.Players.PlayerAdded:connect(function(player)
	repeat wait() until player.Character
local torso = player.Character:WaitForChild("Torso")
local newParticle = game.ServerStorage.ParticleEmitter:Clone()
	newParticle.Parent = torso
local Humanoid = player.Character.Humanoid
Humanoid.HealthChanged:connect(function(NewHealth, player)
	 if NewHealth < 25 then --Checks the new health of the humanoid.
        torso.ParticleEmitter.Enabled = true
    else
        torso.ParticleEmitter.Enabled = false
    end
end)
end)

1 answer

✓ Accepted answer

Just do a Died event on the character's humanoid. I added a LoadCharacter() to make the character respawn instantly after death.

game.Players.PlayerAdded:connect(function(player)
player.Character.Humanoid.Died:connect(function()
char = player.Character
char:LoadCharacter()
char.Torso.ParticleEmitter.Enabled = true
end)
end)

2 comments

0

I get this error (I made it a serverscript) 09:00:15.062 - Workspace.Script:2: attempt to index field 'Character' (a nil value)

0

Your script is correct but you need to wait for the humanoid in the character function. player.Character:WaitForChild("Humanoid").Died:connect(function(char)

This discussion is preserved as it was. New answers, comments, and votes are closed.