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

Why is this function only working once and then suddenly stops working?

Asked by 2 years ago

i have this script that increases a value when the local player dies (Local Script)

Script:

wait(2)
local plr = game.Players.LocalPlayer
local Hum = plr.Character.Humanoid


Hum.Died:connect(function()
    print("You have died. Adding 1+ To Counter.")
    script.Parent.Value = script.Parent.Value + 1
    print("Success")
    plr.PlayerGui.Main.Deaths.TextLabel.Text = script.Parent.Value
end)

print("Death Counter.exe Successfully Started")
0
please say what stops working. all of it? the value?? MightBeAHaxxer 24 — 2y
0
Everything, the function wont fire again after 1 use justinjosh5 19 — 2y

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Each time the player dies it gets a new character and the humanoid connection event no longer runs. You need to hook the humanoids death event to each time the players character is added like I did for you below.

wait(2)
local plr = game.Players.LocalPlayer
local Hum = plr.Character.Humanoid

plr.CharacterAdded:Connect(function(char)
    Hum = char:WaitForChild("Humanoid");

    Hum.Died:Connect(function()
        print("Death")
    end)
end)

print("Death Counter.exe Successfully Started")
0
Please ignore the random semi-colon its a habit I have to add to the end of each line. EnzoTDZ_YT 275 — 2y
0
Thanks i will try it in a min! justinjosh5 19 — 2y
0
1 issue: some reason it doesnt work the first time but then every other death other then the first death works. justinjosh5 19 — 2y
0
but i will mark it as right since i might be able to work around that so good job justinjosh5 19 — 2y
0
Try the humanoid.died connection outside the character added function for the first death ass the wait will end up having the characteradded event ran already. EnzoTDZ_YT 275 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

I would try getting a player script importer like one of those ragdoll or shift2run scripts, and putting that code in it. That way, the script gets distributed to the player and redistributed after death.

Answer this question