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

Why does the health change when i only change the walkspeed?

Asked by
iiKind 13
5 years ago
Edited 5 years ago

I'm trying to make a script that makes a player slow down when damaged. So, if my max health is 100 and my health is 50 and my speed is 16, my walkspeed should become 8. This is the code:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        repeat wait() until character.Humanoid
        local humanoid = character.Humanoid
        local originalSpeed = humanoid.WalkSpeed
        humanoid:GetPropertyChangedSignal("Health"):Connect(function()
            print("health changed")
            humanoid.WalkSpeed = humanoid.Health / humanoid.MaxHealth * originalSpeed
            print("walkspeed updated:", humanoid.WalkSpeed)
        end)
    end)
end)

It works perfectly fine, until this happens:

health changed walkspeed updated: 8 health changed walkspeed updated: 8.1613359451294 health changed walkspeed updated: 8.3214712142944 health changed walkspeed updated: 8.4838285446167 health changed walkspeed updated: 8.6445035934448

why does this happen, and how can i fix it?

0
What is the origional speed? VikingCatto 15 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I think. its probably because of Roblox's auto health regeneration. When a player is damaged, the health will regenerate over time .So that's why your script is firing many times, it because the health keeps changing (increasing/regenerating).

Ad

Answer this question