Instantly terminate a player's momentum

So basically, I’ve set the walkspeed of my game to 2000, but I’ve found that now it takes the player a few seconds to come to a complete stop after releasing their w key. I want the player to come to an instant stop, so I tried setting their HumanoidRootPart’s AssemblyLinearVelocity to 0 every time they stopped trying to move, but it didn’t work. What should I do instead?

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function()
		
		local Humanoid = plr.Character:WaitForChild("Humanoid")
		
		Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			
			if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
				print("Set the velocity to 0")
				plr.Character.PrimaryPart.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
			end
			
		end)
		
	end)
	
end)
3 Likes

Anchor the players HumanoidRootPart then Unanchor it. This should cancle it out.

1 Like

Unfortunately, that doesn’t seem to work.

What do you mean? Can you show me the updated code?

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function()
		
		local Humanoid = plr.Character:WaitForChild("Humanoid")
		
		Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			
			if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
				print("Set the velocity to 0")
				plr.Character.PrimaryPart.Anchored = true
				wait(0.1)
				plr.Character.PrimaryPart.Anchored = false
			end
			
		end)
		
	end)
	
end)

Yeah, here.

What happens when this occurs? Does it print that set volocity to 0?

Yeah, it does. Also, since I put that wait(0.1) in there, you can actually see your character stop for a tiny bit.

Does the just move at the same speed after?

Yeah, pretty much. I don’t think anchoring parts gets rid of the velocity unfortunately.

Try setting the humanoid’s walk speed to 0.

then back to normal walk speed

You need to wait until physics have finished simulating for that frame (with heartbeat) and then set the velocity to 0

game:GetService'RunService'.Heartbeat:Wait()
plr.Character.PrimaryPart.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
7 Likes

Don’t really think that’s doing anything

I’m kind of new to scripting, what does this line do?

game:GetService'RunService'.Heartbeat:Wait()

It wait until after the game has finished calculating physics for that frame

Here is a good explanation:

Thanks for sending me that post. I’ve integrated your suggestion into my script, but it still doesn’t seem to be working properly. Did I put it in correctly?

game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function()
		
		local Humanoid = plr.Character:WaitForChild("Humanoid")
		
		Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
			
			if Humanoid.MoveDirection == Vector3.new(0, 0, 0) then
				print("Setting velocity to 0")
				game:GetService"RunService".Heartbeat:Wait()
				Humanoid.Parent.PrimaryPart.AssemblyLinearVelocity = Vector3.new()
			end
			
		end)
		
	end)
	
end)

On the line after the wait for heartbeat, it says

Vector3.new()

Try

Vector3.new(0, 0, 0)

Seems to work fine for me.

Is your script a server script or a localscript?

1 Like

Server script under ServerScriptService

On the line after the wait for heartbeat, it says

Vector3.new()

Try

Vector3.new(0, 0, 0)

Try this. If this doesn’t work I have one more idea.