Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this local script function only once and does not work after that?

Asked by 5 years ago

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)
0
it's because the values are already set to false on any succeeding run DeceptiveCaster 3761 — 5y
0
it sets them the first time and they stay like that with your script DeceptiveCaster 3761 — 5y
0
But I always change it to test and it does not reset after the first time. Inscendio 42 — 5y
0
try using if statements before changing each value DeceptiveCaster 3761 — 5y
View all comments (4 more)
0
if that doesn't work, capitalize connect() DeceptiveCaster 3761 — 5y
1
use player.CharacterAdded:Wait() instead of a repeat loop User#23365 30 — 5y
0
oh i skipped right over that DeceptiveCaster 3761 — 5y
0
Thanks for your help. Inscendio 42 — 5y

1 answer

Log in to vote
1
Answered by
LeadRDRK 437 Moderation Voter
5 years ago
Edited 5 years ago

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.

Ad

Answer this question