Why does this work only once when a player dies? (Value change)

Hi there,

I have this script that is a Kill Spree leaderstat and resets when you die (It works once), but if you die a second time your Spree value doesn’t reset to 0?

Script:

game.Players.PlayerAdded:Connect(function(player)

local rs = game:GetService(“ReplicatedStorage”)

local char = player.Character or player.CharacterAdded:wait()

local human = char:WaitForChild(“Humanoid”)

local spreestat = player:WaitForChild(“leaderstats”)

– Reset spree

human.Died:Connect(function()

spreestat.Spree.Value = 0

end)

end)

Thanks,

1 Like

hmmm, any errors? if not its ofc a logical error which is hard to fix

OHHHH, i figured it out… since you put the human.died function inside the player added it only will fire and check when a player is added
try adding a while true do @Aozwel

Added that and got "Script timeout: exhausted allowed execution time " Sorry im new to scripting i know this seems obvious

oh u forgot to add a;

wait(0.1)

(cuz while loops is too fast and it crashes if u dont add a wait)

Do a short wait() command so it isn’t checking all the time

1 Like

Dang still didn’t fix the original issue

exactly lol and so it doesnt crash

weird it should fix it chars

Please please PLEASE do not use a loop in your script. This is the most unnecessary thing.
@Jacko_Korbius @WhatDid_HeDo I advised that you carefully understand the purpose of looping. There are better methods than looping.

@OP, the reason it doesn’t reset the 2nd time you die is because PlayerAdded only fires once. When you reset, the character is deleted and you will get a new character. Since you only added the event once, which is after PlayerAdded, the new character doesn’t get the event.

The solution is to add a CharacterAdded. This fires when the Player has a character in the Workspace.

2 Likes

ahhh, sorry my bad im not really good at helping people but im trying my best

Try using this post as it seems someone’s already posted about this. This post also explains how to implement Character Added. Resetting leaderboard timer after death, there are some other replies besides the one linked that should help.

1 Like

Got it thanks to everyone who helped, CharacterAdded was the fix!

3 Likes

so @Aozwel your script should be:
as @Quwanterz said

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(char)

local rs = game:GetService(“ReplicatedStorage”)

local human = char:WaitForChild(“Humanoid”)

local spreestat = player:WaitForChild(“leaderstats”)

– Reset spree

human.Died:Connect(function()

spreestat.Spree.Value = 0

end)

end

end)
1 Like