Movement speed reduction as a percentage of health?

Hello! I had quick question, as the title said. Can you teach me how can I make the movement speed reduce as health? I did some calculation but I just mess up everything. Please help!

1 Like
local Players = game:GetService("Players")

local MAX_SPEED = 16

local hum
local healthChangeConn

local function onHealthChanged(newHealth)
    hum.WalkSpeed = MAX_SPEED * newHealth / hum.MaxHealth
end

local function onCharAdded(char)
    healthChangeConn:Disconnect()
    hum = char:WaitForChild("Humanoid")
    healthChangeConn = hum.HealthChanged:Connect(onHealthChanged)
end

plr.CharacterAdded:Connect(onCharAdded)

The above code is for a local script. You could also do it on the server, but you’d need a slightly more complicated script to do it to every player. If you’re not using Humanoid.WalkSpeed for anything on the server, it’s probably better to just do this on the client.

1 Like

So you could add a remote event. When the player’s health reduces, the remote event will call a script to reduce the player speed. For example -1 health = -1 speed.

1 Like

so I need to place this local script inside the StarterPack?

You should place it into starterplayerscripts.

1 Like