How do i make a Max Player Speed?

Hello! I’m currently trying to make a speed type game, and i need to add a max player speed. I’m stuck on this part and i’m not sure how i should make it. Any help would be very apprechiated! :smiley:

The system
Right now i am just using a simple script that keeps on adding more WalkSpeed each time a “Steps” value increases. I am trying to make a cap for this, say i made the speed cap like 184. Then the player would stop gaining more speed at 184 WalkSpeed. If you need any more details please just message me!

4 Likes

You can use math.clamp for this: math.clamp returns a number between min and max, inclusive.
More information: math | Roblox Creator Documentation

Usage:

Humanoid.WalkSpeed = math.clamp(current_speed, min, max)
4 Likes

Doesn’t this just keep switching between the min and max value?
I would like the player to keep the gained speed and then just make it cap out at a specific amount.

If it’s above max, the speed will be the max speed.
If it’s under min, the speed will be the min speed.

Speed remains the same, unless it’s above or under what you’ve set up.

1 Like

If you do not want the players to go across 184 WalkSpeed, this is pretty simple.

local limit = 184
local plr = game.Players.LocalPlayer
local humanoid = plr:WaitForChild("Humanoid")

humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
    if humanoid.WalkSpeed > limit then
        humanoid.WalkSpeed = limit
    elseif humanoid.WalkSpeed <= limit then
       humanoid.WalkSpeed += 1
    end
end)

(stumbled back on this post & i edited the code to optimize it slightly)

4 Likes