Function Should Cancel When Condition Is No Longer Met

My issue:
I have loops inside function that run and depend on a instance however when this instance no longer exists in the game the loops give out an error.

I know that you can use if statements inside the loops to break them when instance no longer exists however I do not wanna repeat if statements for every loop I make.

Is there any known framework/module that keep these loops running for as long as instance exists?

Well if you want to run a loop for as long as an instance exists, you’re going to need to check if it exists.

You can use .Removing or .AncestryChanged to disconnect RunService connections, but generally you shouldn’t be running into issues with checking the .Parent property of instances.

Also, don’t look for frameworks for hyper-specific tasks, they often overcomplicate things and use more resources than really needed.

1 Like

Don’t bloatware your game wirh frameworks please :pray:
Generally checking

if not instance or not instance:IsDescendant(game) then return end

Should work for you;
If you are talking about connection then you could just disconnect it

Local connection = instance:GetProprtyChanged():Connct(functionHere)

connection:Disconnect()

Could also work however if your instance is destroyed there no need to do that

1 Like

You could also either: spawn a task and cancel it when the instance is destroyed:

local instanceThread = task.spawn(function ()
	while true do
		-- stuff
	end
end)

instance.Destroying:Once(function ()
	task.cancel(instanceThread)
end)

or, just wrap the function in a pcall for a dirty but working result.

2 Likes

Try To Use:

while Instance do
--Stuff
end