Hi,
I was making a slow regain walk script and I ran into a problem. I used lerp and runservice to regain the walk speed but I am having problems cancelling the lerp. For example, I have it so when I am going backwards, it goes to 6 speed and “disconnects” the runservice. Problem is it isn’t disconnecting anything. When I am going back, it is still lerping the speed (I used walkspeed property change to check)
here is the script:
-- controls movement animation
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local LocalPlayer = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local Figure = LocalPlayer.Character
local Humanoid = Figure:WaitForChild("Humanoid")
local MaxSpeed = 13
local WalkLerp
local function lerp(a, b, t)
return a + (b - a) * t
end
function OnDirectionChange()
local RelativeDirection = Humanoid.MoveDirection:Dot(Camera.CFrame.LookVector)
if RelativeDirection > 0.68 or RelativeDirection > 0.75 then -- foward
print("Moving Foward")
Humanoid.WalkSpeed = 10
if not WalkLerp then
print("h")
WalkLerp = RunService.RenderStepped:Connect(function()
if Humanoid.WalkSpeed == MaxSpeed or RelativeDirection < -0.68 or RelativeDirection < -0.75 then
WalkLerp = nil
end
Humanoid.WalkSpeed = lerp(Humanoid.WalkSpeed, MaxSpeed, 0.1)
end)
end
end
if RelativeDirection < -0.68 or RelativeDirection < -0.75 then
print("Moving Backwards")
Humanoid.WalkSpeed = 6
end
end
-- debug
Humanoid:GetPropertyChangedSignal("WalkSpeed"):Connect(function()
print(Humanoid.WalkSpeed)
end)
Humanoid.Running:Connect(OnDirectionChange)