Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a maximum speed limit?

So I have script that increases the characters speed while they run, but I want to have a maximum speed that it cant go above.

Script:

local Running = false

game.Players.PlayerAdded:Connect(function(player)
    repeat wait() until player.Character
    local Humanoid = player.Character:WaitForChild("Humanoid")
        if newState == Enum.HumanoidStateType.Running or 
            newState == Enum.HumanoidStateType.RunningNoPhysics then
            if not Running then Running = true
                repeat wait(0.1)
                Humanoid.WalkSpeed = Humanoid.WalkSpeed + .8
                until not Running
                Humanoid.WalkSpeed = 20
            end
         elseif not Enum.HumanoidStateType.Jumping then
            Running = false
        end
    end)
end)

1 answer

✓ Accepted answer

You could either check if the walk speed was above a certain value before increasing it:

repeat wait(0.1)
if (Humanoid.WalkSpeed < MAX) then Humanoid.WalkSpeed = Humanoid.WalkSpeed + .8 end
until not Running

OR you could automatically cap it by taking either the increased value or the max, whichever is lower:

repeat wait(0.1)
Humanoid.WalkSpeed = math.min(Humanoid.WalkSpeed + .8, MAX)
until not Running

1 comment

0

thank you!

This discussion is preserved as it was. New answers, comments, and votes are closed.