How to detect if part is no longer in GetPartBoundsInBox

Hello, im trying to detect if a part is no longer in bounds of a GetPartBoundsInBox. I’ve tried alternatives with region3, raycasting. However GetPartBoundsInBox has worked best, I just need to know how to check if a part is no longer in GetPartBoundsInBox.

while true do
local filterObjects = {script.Parent}
local boxPosition = CFrame.new(script.Parent.Position)
local boxSize = Vector3.new(10,10,10)
local maxObjectsAllowed = 10
local params = OverlapParams.new(filterObjects,Enum.RaycastFilterType.Blacklist,maxObjectsAllowed,"Default")
local objectsInSpace = workspace:GetPartBoundsInBox(boxPosition,boxSize,params)
for i,v in pairs(objectsInSpace) do
print(v) 
	wait(1) 
	end
end

I tried to make one, but for some reason it only detects parts that enter the box, I don’t know how to fix it.

local BoxCFrame = CFrame.new(script.Parent.Position)
local BoxSize = Vector3.new(10,10,10)
local Params = OverlapParams.new() -- Defaulted to blacklist,
-- also in your script, thats not how to use it.
Params.FilterDescendantsInstances = {  script.Parent }
-- This is how
local removedparts = {}
local lastparts = {}
while task.wait() do -- Checking Repeatedly
	removedparts = {}
	local parts = workspace:GetPartBoundsInBox(BoxCFrame,BoxSize,Params)

	if #parts > 0 and #lastparts > 0 then
		local ispartsdifferent = false
		for _, part in parts do
			local canfind = false
			
			for _, o in lastparts do
				if o == part then
					canfind = true
					break
				end
			end
			
			if not canfind then
				table.insert(removedparts,part)
			end
		end
	end
	
	lastparts = parts
	print(table.unpack(removedparts))
end
1 Like

You need a second loop to compare the last parts against the current iteration’s parts.

local BoxCFrame = CFrame.new(script.Parent.Position)
local BoxSize = Vector3.new(10,10,10)

local Params = OverlapParams.new() -- Defaulted to blacklist,
-- also in your script, thats not how to use it.
Params.FilterDescendantsInstances = { script.Parent }

local lastParts = {}

while true do -- Checking Repeatedly
	local parts = workspace:GetPartBoundsInBox(BoxCFrame,BoxSize,Params)
    local partsAdded, partsRemoved = {}, {}
    for i, part in ipairs(parts) do
        if not table.find(lastParts, part) then
            table.insert(partsAdded, part) -- this part was added
        end
    end

    for i, part in ipairs(lastParts) do
        if not table.find(parts, part) then
            table.insert(partsRemoved, part) -- this part was removed
        end
    end
    
    lastParts = parts -- save the parts from the current iteration to be contrasted with parts from the next iteration
    
    -- now, partsAdded will be a table of parts that are new
    -- partsRemoved will be a table of parts that have been removed since the last iteration

	task.wait(1)
end
1 Like