Character Flinging

So I’ve made a script that boosts a player forwards, however the script also flings the player if they boost into a wall or a fence. Is there any solution to this problem?

1 Like

You could detect for radical changes in the velocity and clamp it within a range

local function ClampVelocity()
    local old_velocity = HumanoidRootPart.Velocity
    local clamped_velocity = Vector3.new(math.clamp(old_velocity.X, min, max), math.clamp(old_velocity.Y, min, max), math.clamp(old_velocity.Z, min, max))
    HumanoidRootPart.Velocity = clamped_velocity
end

RunService.Heartbeat:Connect(ClampVelocity)

Basic example of how to do that. Formatting is bad due to being on mobile

Just to add on to @Fm_Trick, min should be the negative max and max should be the positive max.

local min = -50
local max = 50

This will clamp the player’s max speed per frame to 50 studs a frame.

Sadly the code still didn’t solve the issue.

Did you define the HumanoidRootPart as well? Try lowering the max and min closed to 0?

Please don’t just say the code did not work. Give us the output, errors, and object hierarchy.

I added the code to the script, defined all the variables, and there was no errors yet I still flung.

Try setting the velocity to Vector3(0,0,0) before applying clamped value?

Also don’t forget to do this in a localscript, the server cannot set a player’s velocity as far as I know (you’re probably already doing this).

It’s cool thanks for your help, I solved the issue.

How did you solve the issue? Explain it for whoever has the same problem later on.

13 Likes