Best way to detect if any object is in a region?

I have an area in the workspace, let’s call it Region1. I need the best way to detect if any part is in Region1, so I have an invisible part that covers the entire Region1 (workspace.Region). I need code to run as soon as an object is in Region1, and I need a different code to run when there’s no objects in Region1. Here’s my current Server Script:

local function PartsTouching(Region)
	local connection = part.Touched:Connect(function() end) -- We create a Touched connection so 'Region' will work even if CanCollide is off
	local touching = Region:GetTouchingParts()
	if #touching > 0 then return true end
	connection:Disconnect()
	return false
end

local Region = workspace.Region

while wait() do -- Bad practice
	if PartsTouching(Region) then
		-- Some object is in Region1
	else
		-- No object is in Region1
	end 
end

I think there might be a better way to do this, especially because I’m using while wait()

I think this module made by @ForeverHD is what you’re looking for:

(It’s made for players but I think you could go into the source code and make it compatible with parts if it isn’t already)

2 Likes