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

Why does the player's health go back to 100 after resetting?

Asked by 3 years ago
Edited 3 years ago

I made this script that's suppose to give the player more maxhealth based on their strength so a player with 1000 strength will have more health than someone with 10 strength. Everything works fine except when the player dies or resets their health goes back to 100/100 and the way to fix this is by gaining more strength then their health goes to what it should be.

Here's the script

local DefaultHealth = 100

game.Players.PlayerAdded:Connect(function(player)
    local Stats = player:WaitForChild("leaderstats")
    local Strength = Stats:FindFirstChild("strength")
    local char = player.Character or player.CharacterAdded:wait()
    if char then
        local hum = char:FindFirstChild("Humanoid")
        if hum then
            hum.MaxHealth = DefaultHealth * Strength.Value * 1.25
        end
        Strength.Changed:connect(function(value)
            if player.Character and player.Character:FindFirstChild("Humanoid") then
                player.Character.Humanoid.MaxHealth = DefaultHealth * Strength.Value * 1.25
            end
            player.CharacterAdded:connect(function(character)
                character:WaitForChild("Humanoid").MaxHealth = DefaultHealth * Strength.Value * 1.25
            end)
        end)
    end
end)

I have a healthbar that shows the health going to 100/100 after the player dies so if it could be something wrong in the gui's script let me know if i should post that script here aswell

edit: ok i've tested it and the gui is fine.

1 answer

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago

You would need to set the humanoid MaxHealth and Health when you detect Humanoid.Dead signal and Player.CharacterAdded signal. (You might need to use WaitForChild on Humanoid because HumanoidRootPart load first)

hum.Died:Connect(function()
    char:WaitForChild("Humanoid").MaxHealth = DefaultHealth * Strength.Value * 1.25 -- Since char will automactically yield for the CharacterAdded event, safely use this way
end
0
oh my god "automactically" i meant automatically Xapelize 2658 — 3y
0
Thank you, you were a big help ItzSulfonic 61 — 3y
Ad

Answer this question