Issue with Movement script not working properly

Hello, my movement script is not working properly. The script is meant to slow the player down when they stop walking, however it usually gets caught or causes the player to stop moving.

Demonstrating video below:

Script Below:

local char = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine)
local goal = {WalkSpeed = 0}
local slowing = false
local brick = Instance.new("Part")
brick.Anchored = true
brick.Parent = workspace
brick.Name = "MoveToPart"
brick.Size = Vector3.new(2,2,2)
brick.CanCollide = false
brick.BrickColor = BrickColor.random()

local function move()
	while (char:WaitForChild("HumanoidRootPart").Velocity.X + char:WaitForChild("HumanoidRootPart").Velocity.Z) >= 1 or (char:WaitForChild("HumanoidRootPart").Velocity.X + char:WaitForChild("HumanoidRootPart").Velocity.Z) <= -1 do
		task.wait()
		if char.Humanoid.WalkSpeed <= 22 and slowing == false then
			task.wait(0.05)
			char.Humanoid.WalkSpeed *= 1.015
		end
		if char.Humanoid.MoveDirection == Vector3.zero and char.Humanoid.WalkSpeed >= 16 then
			slowing = true
			char.Humanoid.WalkSpeed = 15
			brick.Position = char:WaitForChild("HumanoidRootPart").CFrame * Vector3.new(0,0,-3)
			char.Humanoid:MoveTo(brick.Position)
			local tween = tweenService:Create(char.Humanoid,tweenInfo,goal)
			tween:Play()
			slowing = false
			task.wait(tweenInfo.Time)
			char.Humanoid.WalkSpeed = 16
			print("done")
		end
	end
end

game:GetService("RunService").Stepped:Connect(move)

I want the movement to slowly slow the player down, like what happens in games like Evade, shown in the video below.


sorry the video is short, the 10mb limit is stopping me from uploading anything good.

Thanks for reading. I hope you can help.

1 Like

Bumping this.

If possible, can someone also give me a hint toward making it work with any walkspeed too? Thanks.

Sorry if i’m making it seem like I want a whole system designed.

The slowing variable is being set back to false immediately after tween:Play().

Since Play() doesn’t wait for the tween to finish your WalkSpeed *= 1.015 code can still run while the tween is trying to bring WalkSpeed down to 0, so they’re fighting each other (probably which led to your infinite drift bug in the video).

Also, move() is connected to RunService.Stepped but contains a while loop, which means a new move() loop can start every frame and overlap with the previous ones.

If you clean up some of these issues, you could store the players walkspeeds as a variable initially or do something like:
local defaultWalkSpeed = game:GetService(“StarterPlayer”).CharacterWalkSpeed

If you wish to change the default walkspeed in your game later

Hope this helps :slight_smile:

1 Like

I have took your advice with the RunService.Stepped and the sloving variable being set, but the character still pauses. Also, could there be a way I can replace the moveto with a script that slowly moves the player forward and allowing for rotation like in the mentioned games?

NEW CODE

local char = script.Parent
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5,Enum.EasingStyle.Sine)
local goal = {WalkSpeed = 0}
local slowing = false
local brick = Instance.new("Part")
brick.Anchored = true
brick.Parent = workspace
brick.Name = "MoveToPart"
brick.Size = Vector3.new(2,2,2)
brick.CanCollide = false
brick.BrickColor = BrickColor.random()

while true do
	task.wait()
	while (char:WaitForChild("HumanoidRootPart").Velocity.X + char:WaitForChild("HumanoidRootPart").Velocity.Z) >= 1 or (char:WaitForChild("HumanoidRootPart").Velocity.X + char:WaitForChild("HumanoidRootPart").Velocity.Z) <= -1 do
		task.wait()
		if char.Humanoid.WalkSpeed <= 22 and slowing == false then
			task.wait(0.05)
			char.Humanoid.WalkSpeed *= 1.015
		end
		if char.Humanoid.MoveDirection == Vector3.zero and char.Humanoid.WalkSpeed >= 16 then
			slowing = true
			char.Humanoid.WalkSpeed = 15
			brick.Position = char:WaitForChild("HumanoidRootPart").CFrame * Vector3.new(0,0,-3)
			char.Humanoid:MoveTo(brick.Position)
			local tween = tweenService:Create(char.Humanoid,tweenInfo,goal)
			tween:Play()
			task.wait(tweenInfo.Time)
			slowing = false
			char.Humanoid.WalkSpeed = 16
			print("done")
		end
	end

end



Instead of using a Tween to slow down the player, use a lerp function:

function lerp(start, goal, alpha)
    return start + (goal - start) * alpha
end

This will smoothly reduce the player’s speed without completely stopping their movement.

this is a script i made a WHILLEEEE back for the same purpose, i added comments and tweaked some stuff here and there, if theres any errors please let me know!

--assuming this is in StarterCharacterScripts
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char.Humanoid
local hrp = char:WaitForChild("HumanoidRootPart")
local newMoveDirection
local lastMoveDirection = Vector3.zero

game:GetService("RunService").RenderStepped:Connect(function()

	newMoveDirection = humanoid.MoveDirection.Magnitude

	if newMoveDirection > 0.1 then --if the player is moving
		lastMoveDirection = humanoid.MoveDirection -- then update the moveDirection var
	end

		--if you perfer using the camera's direction to move
		--the player forwards instead of their MoveDirection, then use this:
--local forward = Vector3.new(workspace.CurrentCamera.CFrame.LookVector.X, 0, workspace.CurrentCamera.CFrame.LookVector.Z).Unit
--use humanoid:Move(forward) in place of humanoid:Move(lastMoveDirection)

	if hrp.Velocity.Magnitude > 0.2 and humanoid.WalkSpeed > 3 and newMoveDirection < 0.1 then
		humanoid:Move(lastMoveDirection)
		humanoid.WalkSpeed *= 0.95 -- the lower the number the faster the player stops
	else
		humanoid.WalkSpeed = 16 --replace this with the players default walkspeed, any number works here
	return end
end)

I think you should extend the waiting time for the tween to complete because sometimes

Say if the tween takes 0.5 seconds to complete and you task.wait(0.5) the tween may take longer than 0.5 seconds or task.wait() isn’t accurate.

So setting the walkspeed to 16 RIGHT AFTER in your case, the tween hasn’t finished. So the tween sets it back to 0.

So maybe increase the waiting time by 0.1 seconds or less?

hello, this works just fine!

thanks for your help. because i want to credit everyone in my game, would you like to be added to the credits or stay anonymous?

1 Like

Glad it works! You don’t have to credit me, I’m just happy I could help❗

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.