Connections don't work inside functions?

So I’m trying to figure out a way to cancel a function instantaneously when a value is changed, but it doesn’t seem to be working… Here’s the basic code. Spawn function is a whole mumbo jumbo of irrelevant code to this issue.

	function RunCollectionMove(finalPosition,unit,FaceDirection)
		
		TaggedPart.Parent.Properties.StopAllOrders:GetPropertyChangedSignal("Value"):Connect(function()

			print ("OH NO, CRINGE!")

			return
		end)
		
		spawn(function()
			
			if SpinTween then
				SpinTween:Cancel()
			end
			
			unit.HumanoidRootPart:SetNetworkOwner(nil)

			wait(math.random(100,300)/100)
			
			repeat wait()
				unit.Humanoid:MoveTo(finalPosition) 
			until (unit.HumanoidRootPart.Position - finalPosition).magnitude < 5

			wait(5)
			
			repeat wait()
				unit.Humanoid:MoveTo(finalPosition) 
			until (unit.HumanoidRootPart.Position - (finalPosition + Vector3.new(0,2,0))).magnitude < 3

			wait(math.random(100,500)/100)

			local info = TweenInfo.new(2,Enum.EasingStyle.Sine)

			local CFrameValue = Instance.new("CFrameValue")
			CFrameValue.Value = unit:GetPrimaryPartCFrame()

			CFrameValue:GetPropertyChangedSignal("Value"):Connect(function()
				unit:SetPrimaryPartCFrame(CFrameValue.Value)
			end)

			local Angle = FaceDirection

			SpinTween = TweenService:Create(CFrameValue, info, {Value = (Angle - Angle.Position) + unit.HumanoidRootPart.Position})

			SpinTween:Play()

			SpinTween.Completed:Connect(function()
				Tighten(unit)
			end)
		end)
	end

I can get why this can’t be possible, but there has to be a way for me to cancel this function.

1 Like

i have a health regeneration system, and when the player gets 100 hp or Die, i’ll break the loop

Example only, you can adapt it

“cancel function instantaneously when a value is changed”

local function Regen()
	
	local ActiveRegen = false
	local Dead = false
	
	if health < 100 and ActiveRegen == false then -- checking if player has less than 100 hp
		
		ActiveRegen = true
		
		repeat 
				
			if health >= 100 or Dead == true then -- if suddently the player gets 100 hp while the regen is active then it will break ( ActiveRegen is already active because this if statement is repeating only when RegenActive is True )
--or if the user Died, somehow
					
				break-- will stop the Repeat loop			
			end
				
	         task.wait(1)
             health = health + 1
		until

		health >= 100
		
	end
end

Remove the spawn, see if you have an error as I believe spawn and coroutines suppress errors

So I can have a connection inside a function?

I have to have the spawn. Trust me it’s because this is a multiple AI operating script

Yes, i guess, do you mean something like this?


local function PlayerDied() 
	-- if you put Below Health.Changed function, it will not work,  
	--but if you change to "function" and not "local function" then it will activate from anywhere and whenever the PlayerDied() fires
	
	--So? i see that the player died... i'll warn the server then 
	RemoteEvent:FireServer()
	
end


Health.Changed:Connect(function()

	if Dead == true then
		print"Activated"
		PlayerDied() -- this will activate another function somewhere	
	end
end)

Yes, except playerdied would be inside the function