Detect when player stops touching a part while using :GetPartBoundsInBox method?

  1. What do you want to achieve?
    I’ve recently been looking into more ways to optimize collision inside of my code, and I’ve been wondering how I could detect whenever a part exits another part’s collision.
  2. What is the issue?
    I’ve tried using conditional statements and table.find to check if a Humanoid exists inside of the partsInPart table as seen below.
local function musicTriggerInterior(partToBeHit)
	-- Creates a table which contains all of the parts that have overlapped with partToBeHit bounding box.
	local partsInPart = workspace:GetPartBoundsInBox(partToBeHit.CFrame, partToBeHit.Size)
	
	-- Loops through the parts that are inside of partToBeHit bounding box.
	for i, touchingPart in pairs(partsInPart) do
		local humanoid = touchingPart.Parent:FindFirstChild("Humanoid")
		
		if table.find(partsInPart, humanoid) ~= nil then -- Intended to check if the humanoid does exist in the table
			print("A")
			print(partsInPart)
		elseif table.find(partsInPart, humanoid) == nil then -- Intended to check if the humanoid doesn't exist in the table
			print("B")
			print(partsInPart)
		end
		
		print(table.find(partsInPart, humanoid))
	end
	
end

while task.wait(5) do 
	musicTriggerInterior(workspace.Trigger_Area) -- Trigger_Area is a part
end

I’ve tried multiple variations of this code, but none of them are seeming to work.

  1. What solutions have you tried so far?
    I’ve looked at a couple of similar forum posts regarding bounding box collision, and I’ve taken a look at the pages for the methods used, as well as the OverlapParams page (Although I am not sure if it is needed in this situation).
1 Like