How to detect a player's speed

Hello! I have a sliding system, and I want the player to stop sliding when their speed goes below a certain number, for example, when they crash into a wall. However, checking the assembly linear velocity does not work, and I think it is because the sliding system creates a linear velocity inside the player’s humanoid root part, and the it affects the player’s assembly linear velocity. Without using a loop to check the difference between a player’s position before and after x seconds, how would I determine the speed of the player?

Use Humanoid.Running

Humanoid.Running is not accurate, i have noticed people meeting issues with this.


You would prob need to use Humanoid.MoveDirection, this can let you know if the player is moving, and also which direction they are walking to.

Yes, but .Running is easier to understand than .MoveDirection

What do you mean it doesn’t work? Does it return an empty vector3?

you could try and get the HumanoidRootPart.Velocity.Magnitude, although I’m not sure if this would work in the context you provided. but that’s deprecated so it’s probably not smart

1 Like

Why can’t you use a loop? I can’t think of a reason you absolutely couldn’t use one.

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

local lastPosition = humanoidRootPart.Position
local speedThreshold = 10 -- Set your desired speed threshold
local checkInterval = 0.1 -- Time interval between checks (in seconds)

-- Function to calculate speed in studs per second
local function calculateSpeed()
    while true do
        -- Wait for the set interval
        wait(checkInterval)

        -- Get the current position of the player
        local currentPosition = humanoidRootPart.Position
        
        -- Calculate the distance traveled
        local distance = (currentPosition - lastPosition).magnitude
        
        -- Calculate the speed (studs per second)
        local speed = distance / checkInterval
        
        -- Output speed or stop sliding if below threshold
        print("Player speed: " .. speed .. " studs/second")
        
        if speed < speedThreshold then
            -- Stop the player from sliding, add your sliding stop logic here
            print("Player sliding stopped due to low speed")
            -- Example: humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
        end

        -- Update the last position for the next check
        lastPosition = currentPosition
    end
end

-- Start checking the player's speed
calculateSpeed()

3 Likes

How inaccurate is Humanoid.Running? Because it works out fine for me. Also Humanoid.MoveDirection only provides the direction of the character, not the speed. I don’t want the player to stop sliding when they stop moving completely, but when their speed goes below a certain number.