Cancelling a lerp runservice connection

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)
	

You never called disconnect on WalkLerp though?

image
shouldn’t it be in this?

image
Or disconnect it here before setting the value to nil.

1 Like

Doesn’t changing a connection to nil already disconnnect it? I tried disconnecting it but tha didnt work so I switched to that instead.

OH heh, no GC doesn’t automatically clear connections when you set it to nil. You have to do connection:Disconnect() then it’ll do the rest.

1 Like

I’ll try that tomorrow, I’m not sure if it will help though. Thanks

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