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

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

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")

2 comments

0

please say what stops working. all of it? the value??

0

Everything, the function wont fire again after 1 use

2 answers

✓ Accepted answer

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")

5 comments

0

Please ignore the random semi-colon its a habit I have to add to the end of each line.

0

Thanks i will try it in a min!

0

1 issue: some reason it doesnt work the first time but then every other death other then the first death works.

0

but i will mark it as right since i might be able to work around that so good job

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.

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.

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