Connection not disconnecting

Hello,

I use this function for flying in my game (sorry for the mess)

Summary
local Flying = false																	-- the functions that set flying = false still run after
																				
local flycon
flycon = 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 latchcon	
			local touchcon
			
			jumpcon = UIS.InputBegan:Connect(function(input, gameProcessed)
				if input.KeyCode == Enum.KeyCode.Space then
					Flying = false
					print("flying false do to jump")
				end
			end)	
			
			
			
			latchcon = game:GetService('RunService').Heartbeat:Connect(function()
				flylatch()
				if latchvalue == true then
					print("flying false due to big PP")
					Flying = false
				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")
						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)
					
					local Gravity = Controller.GravityUp
					if (Gravity-Vector3.new(0, 1, 0)).Magnitude > .1 then
						Flying = false
						print("flying false do to gravity")
							latchcon:Disconnect()
							jumpcon:Disconnect()
							touchcon:Disconnect()
					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)

It is very long as it gives multiple ways for the flying state to end, and multiple checks for errors.

Basically, it works great and flying ends when it is supposed to, but the Connections that do the checks like touchcon, jumpcon, latchcon do not end when Flying = false.

It doesn’t look like theres anything looking for Flying being set to false, and disconnecting the connections accordingly. Try checking for it inside one of those connections (touchcon, jumpcon, etc).

1 Like

So you mean like this?

Summary
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

I previously used this code but still the issue where they continued running after being disconnected.

EDIT:

Sorry that I forgot to include but these only should only run if the player is not on the ground and Flying == true.

Summary
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

So shouldn’t since Flying is being set to false, these should not be working based off of that?