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

Help with fixing my leaderstats reset on death script?

Asked by 2 years ago

I am making a survival game and I have a script that resets the players leaderstat on player death but it resets all leaderstats instead of just my time leaderstat called "Timex" here is my script:

01local function onPlayerAdded(plr)
02    local con1
03    plr.CharacterAdded:Connect(function(char)
04        repeat wait() until char.Parent and char:FindFirstChild("Humanoid")
05        con1 = char.Humanoid.Died:Connect(function()
06            for i, v in pairs(plr.leaderstats:GetChildren()) do
07                if tostring(v.ClassName):find("Value") then
08                    v.Value = 0
09                end
10            end
11        end)
12        repeat wait() until not char.Parent
13        con1:Disconnect()
14    end)
15end
16 
17game.Players.PlayerAdded:Connect(onPlayerAdded)

Thanks.

1 answer

Log in to vote
1
Answered by 2 years ago

Firstly, you do not need to store the humanoid died connection in a variable because all connections tied to an instance are automatically disconnected as soon as the instance is destroyed.

Secondly, the "ClassName" property of any instance is already a string, making it useless to convert it again to string.

Thirdly, your code assumes that any instance with the word "Value" in it's "ClassName" property is an int value or number value which isn't the case for the other instances that has the word "Value" in its "ClassName" property. (E.g "BoolValue", "StringValue", "Vector3Value", "ObjectValue", etc)

Lastly, you only need to check if the "Name" property of each object is "Timex" since you stated that you only want to reset your time leaderstat called "Timez"

That being said, lets fix your code:

01local function onPlayerAdded(plr)
02 
03    plr.CharacterAdded:Connect(function(char)
04        repeat task.wait() until char.Parent and char:FindFirstChild("Humanoid")
05         char.Humanoid.Died:Connect(function()
06            for i, v in pairs(plr.leaderstats:GetChildren()) do
07                if v.Name == "Timex" then
08                    v.Value = 0
09                    break
10                end
11            end
12        end)
13        repeat task.wait() until not char.Parent
14 
15    end)
16end
17 
18game.Players.PlayerAdded:Connect(onPlayerAdded)

Hope I helped!

Ad

Answer this question