How to create a connection of a function

Wassuh,

I have this function:

Summary
function latch()																													
	
	local origin = HRP.Position
	local direction = HRP.CFrame.LookVector * REACH						

	local result = Workspace:Raycast(origin, direction, rayParams)
	if result then
		results = result.Normal
		pos = result.Position
		typ = result.Instance
		pcframe = CFrame.new(pos)
		SurfaceCFrame = CFrame.new(result.Position, result.Position+result.Normal)*CFrame.Angles(math.rad(-90),0,0)
	end
	
	if results == nil then
		return
	end
	
	if results ~= nil then
		print("normal latching began")
		latching()
	end
end

If you are wondering I use it to control the player gravity and allow them to walk on walls.

However that is not what this post is about; Instead I basically want to know how I can create a connection for this function.

You see I use this when the player is flying so they can latch to any near by object; and so they don’t touch them and bug out.

However as you can see in this GIF, the function works, but then keeps firing after the player has already latched, and it lasts a few seconds.

I use this function inside of the function that I use for flying, so I have tried setting con = latch(), but since I disconnect the whole function after the flying ends, it causes errors.

If anyone could get me moving on the right track, or link me to something helpful, that’ll be great!

I think I’m a bit confused on what you’re trying to accomplish. It seems like you’re saying that the function keeps on firing more than it is supposed to? I’d be curious to see the rest of your code to see more context.

Connections are made on events, not functions, so it might mean a different solution could work better.

Right so it plays inside of my flying function.

I don’t want to post the entire function because it is very long with many checks that set flying = false.

However, here is the part where I use latch()

Summary
local Connection
			
		Connection = game:GetService('RunService').Heartbeat:Connect(function()
			local Suc, Err = pcall(function()
				if Flying == true then
					BV.Velocity = Vector3.new(0,0,0)
					BV.Velocity = Cam.CFrame.LookVector * 50

					wait(.5)
					flylatch()
					
					local Gravity = Controller.GravityUp
					if (Gravity-Vector3.new(0, 1, 0)).Magnitude > .1 then
						Flying = false
						print("flying false do to gravity")
					end	
					
				else
					Connection:Disconnect()
					BV:Destroy()	
					character.Animate.Disabled = false
					humanoid.JumpPower = 0
				end
			end)

			if not Suc and Err then
				print(Err)
				Connection:Disconnect()
				BV:Destroy()
				character.Animate.Disabled = false
				humanoid.JumpPower = 0
			end
		end)

I have it inside of a RunService.Heartbeat so that it will continuing firing, on top of that it only fires when Flying == True.

Like I said I use other functions that set Flying = false such as if the player jumps, .Touched, Gravity Changed.

So my first thought was that latch() should not be playing after Flying == false. But you can see in the GIF, even after the player has landed (.Touched and Gravity Changed btw), the function still fires for a second or so after:
https://gyazo.com/2887ad44d4308aa4b20c88ee79fa52f1

An even better example can be seen when the player latches to the something flat instead:
https://gyazo.com/c51b5a369d15a858ee05ccd6c10723df

You can see that the player cannot walk immediately because of this, which is bad because my game relies on fast movement and reaction.

EDIT:

I figured I better just include the entire code incase the error may be with how it is structured:

Summary
local Flying = false																	-- the functions that set flying = false still run after
																				

humanoid:GetPropertyChangedSignal("JumpPower"):Connect(function(key)
	if humanoid.JumpPower == 100 then
		delay(5, function()
			Flying = false
		end)
		Flying = true
		print("Power Change Acknowledged")
		wait(.5)
		print("Flying State Began")

		Flying = true

		character.Animate.Disabled = true
		-- ADD FLYING ANIMATION HERE

		local BV = Instance.new("BodyVelocity", character.PrimaryPart)
		BV.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		BV.Velocity = Vector3.new(0,0,0)
		BV.Name = "FlightForce"
		
		if StateTracker.State ~= "onRunning" and Flying == true then
			print("State good")
			local jumpcon
			local touchcon
			
			jumpcon = UIS.InputBegan:Connect(function(input, gameProcessed)
				if input.KeyCode == Enum.KeyCode.Space then
					Flying = false
					print("flying false do to jump")
					jumpcon:Disconnect()
				end
			end)	
			
			
			for i, v in pairs(character:GetChildren()) do 
				if v:IsA("BasePart") then
					touchcon = v.Touched:Connect(function(touch)
						if not character:FindFirstChild(touch.Name) then
							Flying = false
							print("flying false do to touch")
							touchcon:Disconnect()
						end
					end)
				end
			end
		
			local Connection
			
			Connection = game:GetService('RunService').Heartbeat:Connect(function()
				local Suc, Err = pcall(function()
					if Flying == true then
						BV.Velocity = Vector3.new(0,0,0)
						BV.Velocity = Cam.CFrame.LookVector * 50

						wait(.5)
						flylatch()
					
						local Gravity = Controller.GravityUp
						if (Gravity-Vector3.new(0, 1, 0)).Magnitude > .1 then
							Flying = false
							print("flying false do to gravity")
						end	
					
					else
						Connection:Disconnect()
						BV:Destroy()	
						character.Animate.Disabled = false
						humanoid.JumpPower = 0
					end
				end)

				if not Suc and Err then
					print(Err)
					Connection:Disconnect()
					BV:Destroy()
					character.Animate.Disabled = false
					humanoid.JumpPower = 0
				end
			end)
		end	
	end
end)