Only one heartbeat loop disconnects

upon calling startSmoke on more than one object the object that isn’t the first doesn’t disconnect its loop when called for and kinda just lingers around for a little bit.

05238d6c2f483746a7fd4532781c3c59

function module:startSmoke(part,decayTime,sizeAmplification, originalColor, fadeto)

	spawn(function()
		
		local elapsed = 0

	  >	smokeLoop = game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
		end)

		part.AncestryChanged:Connect(function()

			smokeLoop:Disconnect() -- DOESN'T CANCEL ON THE SECOND

			
		end)
		
	end)

end

Where is the smokeLoop defined? Use local smokeLoop to ensure, that the variable will exist only in current context, and reference won’t be changed.
The issue might be, that you’re creating smokeLoop variable in module context, which means that that first startSmoke will set smokeLoop = 0x01, second call will set smokeLoop = 0x02, and when AncestryChanged signal is called, it will disconnect 0x02 in both parts. Because smokeLoop is referencing 0x02.

local smokeLoop = ...
part.AncestryChanged:Connect(function()
    smokeLoop:Disconnect()
end)

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