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

How to increase health based off an stat value?

Asked by 3 years ago
Edited 3 years ago

I want to make it so when a player gains strength their health also goes up. I'm a new scripter so I have no idea what I'm doing and I'm stuck.

This is what i tried so far but the health does no go up, nothing in output either.

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

update: I had this script above in a local script under starter character so i moved it into a script under server script and when i tested, the players maxhealth went to 0.

0
maybe the Strength.Value is 0? prooheckcp 340 — 3y
0
the player has over 2000 strength tho so how can that be? ItzSulfonic 61 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

If you have a script that doesn't increase the strength value, the value is being multiplied by zero. Add a script that increases the strength value, and you might be rid of the error.

0
i have a button that gives strength just for testing and no matter how much strength i have the health doesn't go up ItzSulfonic 61 — 3y
0
oh MrBarfSquirrel 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Got it working, thanks for the help.

updated 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
        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 = 100 + Strength.Value * 1.25
            end)
        end)
    end
end)

Answer this question