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 local script function only once and does not work after that?

I'm trying to make a local script that resets some items in the player whenever they die. But for some reason, it functions only the first time the player dies and won't function after that.

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:connect(function()
	player.LegBroken.Value = false
	player.Bleeding.Value = false
	player.PlayerScripts.SprintScript.Disabled = false
end)

8 comments

0

it's because the values are already set to false on any succeeding run

0

it sets them the first time and they stay like that with your script

0

But I always change it to test and it does not reset after the first time.

0

try using if statements before changing each value

0

if that doesn't work, capitalize connect()

1

use player.CharacterAdded:Wait() instead of a repeat loop

0

oh i skipped right over that

0

Thanks for your help.

1 answer

✓ Accepted answer

A player’s character always deletes when it dies. This means that the Humanoid along with the other character’s objects will be deleted and a new character model spawns instead. To correct this, you must connect the event everytime that the character respawns:

player.CharacterAdded:Connect(function(character)
      character:WaitForChild("Humanoid").Died:Connect(function()
             -- do stuff
      end)
end)

Also you don’t need to define character and humanoid first.

Hope this helps.

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