How can I cancel a loop that applies to multiple objects for only one object?

  1. What do you want to achieve?
    I have function which activates a Heartbeat Event for the object, given in the function. The reason for that is, that I have to keep an unanchored object at the same height, therefore I am using Heartbeat. I want to stay aways from anything that uses physics, because that would be too laggy because of the amount of objects.

  2. What is the issue?
    The issue is that, when I want to move that Model, I have to cancel this heartbeat loop, but only for some models/objects, because I don’t move all at once. I currently have no idea, how I can cancel this loop, only for specific objects at the time.

try this

local RunService = game:GetService("RunService")
local CollectionService = game:GetService("CollectionService")
local FloatingTag = "Floating"

RunService.Heartbeat:Connect(function()
	for _, object in pairs(CollectionService:GetTagged(FloatingTag)) do 
		-- do whatever u do to keep it in the air
	end
end)

--[[
use :AddTag to add the tag to make it float 
ie: CollectionService:AddTag(object,"Floating")

and use :RemoveTag to make it not float anymore
ie: CollectionService:RemoveTag(object,"Floating")
]]
1 Like

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