How to check all Models touching a part

Im making a sell thing in my game, and i wanted to know how i could make all the models on a platform be “Sold”
The part im stuck on is how to detect ALL items on the platform, i know .touched only returns one result, so how do i get all parts touching the platform?

1 Like

You can try Part:GetTouchingParts() to get all physically touching parts. “Physically” is in bold, because if the CanCollide property of a part is false, it would not be detected using this method. Here’s some example code of how it should work:

local SellPart = game.Workspace:WaitForChild("Part")

local TouchingParts = {}

while task.wait() do
	for Index, TouchingPart in pairs(SellPart:GetTouchingParts()) do
		if TouchingPart ~= nil then
            if not TouchingPart.Parent:IsA("Model") then return end

			if not table.find(TouchingParts, TouchingPart) then
				table.insert(TouchingParts, TouchingPart)
			end
		end
		
		for Index, Part in pairs(TouchingParts) do
			if table.find(TouchingParts, Part) and Part ~= nil and TouchingPart.Name ~= Part.Name then
				table.remove(TouchingParts, tonumber(TouchingParts[Part.Name]))
			end
		end
	end
end

how ca i get all Models though? wont “touchingparts” give duplicates of the models? idk

i see you changed it, does this account for parts being removed?

Oh yeah, my bad. I just updated the code, so it shouldn’t give duplicates. To check if it’s a model, you can just check if the parent is a model.

if TouchingPart.Parent:IsA("Model") then
-- do stuff here
end

Just read your latest message, it doesn’t update if the part is removed, I’ll update it soon though.

EDIT: Just updated it! My previous post should be good now, and I solved all the issues :slight_smile: !

thank you so much, i can do the rest of the code for actually selling it

Please mark his post as “Solution Found”

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