Cant disconnect a RBXScriptConnection

I was trying to make a sliding module, and to stop sliding, I needed to disconnect a RBXScriptConnection. The problem is that when I run the Disconnect function for the connection, it doesn’t seem to do anything.

here’s the connection that is supposed to be disconnected

postmovementevt = RS.Heartbeat:Connect(function(dt)
			timesincesstart += dt
			
			local linervel = hrp.AssemblyLinearVelocity
			hrp.AssemblyLinearVelocity = linervel - floornormal * linervel:Dot(floornormal)
			
			ao.CFrame = CFrame.lookAt(Vector3.zero,hrp.AssemblyLinearVelocity)
			
			if timesincesstart > 0.2 then
				if math.abs((hrp.AssemblyLinearVelocity * Vector3.new(1,0,1)).magnitude) < 0.1 then
					reset() -- supposed to disconnect the conncection
					return
				end
			end
		end)

and here’s is where it’s disconnected does

local function reset()
		if onhit._connected then
			onhit:Disconnect() -- seems to work just fine (i think)
		end
		
		if postmovementevt.Connected then
			postmovementevt:Disconnect() -- doesn't seem to do anything
		end
		
		GF:stopAnimation("slideanimation",hum.Animator)
		slidinghitbox:HitStop()
		ao.Enabled = false
		hum:ChangeState(Enum.HumanoidStateType.GettingUp)
	end

Any help is appreciated. If more information is needed, please let me know.

4 Likes

Can I see the full script? It seems like from my POV that your code has a lot of errors.

1 Like

sure, here’s the full script:

function SlidingMod.Slide(player: Player,machine)
	Rep.Remotes.Functions.AttachSlideHitbox:InvokeServer()
	
	local slideanimation = game.ReplicatedStorage.Animations.Movement.slideanimation
	local slideacc = 5
	
	local slidinghitbox = playerhitboxen[player.Name]
	local char = player.Character
	local hrp:Part = char.HumanoidRootPart
	local hum:Humanoid = char.Humanoid
	local floornormal
	local postmovementevt: RBXScriptConnection
	local onhit
	local ao: AlignOrientation = hrp:FindFirstChild("SlideOrientation")
	local timesincesstart = 0
	
	local function reset()
		if onhit._connected then
			onhit:Disconnect()
		end
		
		if postmovementevt.Connected then
			postmovementevt:Disconnect()
		end
		
		GF:stopAnimation("slideanimation",hum.Animator)
		slidinghitbox:HitStop()
		ao.Enabled = false
		hum:ChangeState(Enum.HumanoidStateType.GettingUp)
	end
	
	onhit = slidinghitbox.OnHit:Connect(function(part, _, result:RaycastResult)
		slidinghitbox:HitStart()
		floornormal = result.Normal
		local groundslope:Vector3 = -char:WaitForChild("HumanoidRootPart").CFrame.RightVector:Cross(floornormal)
		
		if groundslope.y >= 0.1 then
		end
		
		ao = hrp:FindFirstChild("SlideOrientation")
		ao.Enabled = true
		hum:ChangeState(Enum.HumanoidStateType.Physics)
		
		GF:playAnimation(slideanimation,hum.Animator)
		
		local forwardforce = groundslope.Unit * slideacc * hrp.AssemblyMass
		hrp:ApplyImpulse(forwardforce)
		
		postmovementevt = RS.Heartbeat:Connect(function(dt)
			timesincesstart += dt
			
			local linervel = hrp.AssemblyLinearVelocity
			hrp.AssemblyLinearVelocity = linervel - floornormal * linervel:Dot(floornormal)
			
			ao.CFrame = CFrame.lookAt(Vector3.zero,hrp.AssemblyLinearVelocity)
			
			if timesincesstart > 0.2 then
				if math.abs((hrp.AssemblyLinearVelocity * Vector3.new(1,0,1)).magnitude) < 0.1 then
					reset()
					return
				end
			end
		end)
	end)
	
	slidinghitbox:HitStart()
end
1 Like

Your onhit event is probably being fired multiple times which’ll overwrite the connection making you unable you to disconnect it, try add a debounce and see if that fixes it

3 Likes

Try checking to see if your connection is actually disconnecting.

if postmovementevt.Connected then
	postmovementevt:Disconnect() -- doesn't seem to do anything
    print(postmovementevt.Connected)
end

Side note: If you are seeing that there are multiple results (EX: You’re constantly seeing “true/false” being delivered to the output multiple times), then that means that @shadowmaster940 is most likely correct in their saying that the event is being fired multiple times. If you see “false”, that will mean that your connection is not connected. If you see true, however, then that means that your connection did not disconnect properly.

2 Likes

This works, sometimes. Only when I’m on flat ground. When going down slopes it no longer works.

debounce function:

local function debounce(numofframestowait)
		canrun = false
		coroutine.wrap(function()
			for i = 1, numofframestowait do
				RS.Heartbeat:Wait()
			end
			canrun = true
		end)()
	end
1 Like

That’s the weird thing, I’ve already done this and for some reason it only prints false, I’ll check again, just to make sure.

Edit: Just checked again, and it definitely only prints false

1 Like

This is probably what is happening, though a debounce probably wouldn’t be the solution here, instead you should probably store a variable that states that you’re sliding and use that as a guard clause for the OnHit function.

    local sliding = false
	onhit = slidinghitbox.OnHit:Connect(function(part, _, result:RaycastResult)
        if sliding then return end
        sliding = true

        ... 

Then inside the reset function, just simply set sliding to false. Alternatively, you could check if the connection is already connected to the postmovementevt variable and use that for the guard clause.

2 Likes

This does work, but doing this causes the event to only run once, and I need it to run multiple times so that things like the floor normal get updated.

1 Like

Then consider putting only the connection inside behind the guard clause / whatever functions should only be run at the start of running

2 Likes

Worked like a charm, thank you very much :slight_smile:.

2 Likes

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