Touch Ended like function for GetPartsInPart or GetTouchingParts

The title speaks for itself

Is there a Touch Ended like function for GetPartsInPart or GetTouchingParts?
If not, how can i make it myself?

Okay so i found out a way to make a Touch Ended function for GetPartsInPart, turned out to be pretty simple

local Touching = {}

while wait() do
	local overlapParams = OverlapParams.new()
	overlapParams.FilterType = Enum.RaycastFilterType.Exclude
	overlapParams.FilterDescendantsInstances = {script.Parent}
	
	local parts = workspace:GetPartsInPart(script.Parent, overlapParams)
	
	for _,v in ipairs(parts) do
		if not table.find(Touching, v) then
			table.insert(Touching, v)
			print(v.Name.." is intersecting with the part")
		end
	end
	
	for _,v in ipairs(workspace:GetDescendants()) do
		if v ~= script.Parent then
			if table.find(Touching, v) then
				if not table.find(parts, v) then
					table.remove(Touching, table.find(Touching, v))
					print(v.Name.." is no longer intersecting with the part")
				end
			end
		end
	end
end

for some reason the output isnt shown on the video, weird.

You can make it yourself by constantly comparing a new list of parts with an old one that represents previous presences of parts.

Assuming regionPart is the ‘container’ we consider:

local function onPartLeft(leaver)
	--code
end

--

local runSvc = game:GetService'RunService';
local regionPart: BasePart;

local currentParts = {};	--cache parts from previous iteration

while runSvc.Heartbeat:Wait() do
	local newParts = workspace:GetPartsInPart(regionPart);
	
	for i, currentPart in currentParts do
		for _, newPart in newParts do
			if currentPart == newPart then
				currentParts[i] = nil;	--remove part from cache if also present in this iteration
				break;
			end
		end
	end
	
	for _, leaver in currentParts do	--currentParts only contains parts that werent present this iteration
		onPartLeft(leaver);	--fire our emulation of an event
	end
	
	currentParts = newParts;	--update cache
end

The comments help clarify some behavior that isn’t immediately obvious. I should probably note that this might not be the most performant way of achieving this since each iteration is worse than O(n2).

nevermind…

edit: added break in the nested for loop for optimization and clarity (if someone knows whether table.find is more efficient tell me)

1 Like

hey thanks for your effort, this also can help so ill like your reply :slight_smile:

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