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.
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
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
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)