Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't my script print the touching parts?

Here is my script:

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local gett = script.Parent:GetTouchingParts()
	for i,v in pairs(gett) do
		print(v.Name)
	end
end)

It doesn't work.

4 comments

0

Oh wait. The part had cancollide off. Is there a way to get the touching parts if the part has cancollide off?

0

if it is a block and you didn't rotate it, you can try region3

0

if it is a block and you didn't rotate it, you can try region3

0

magnitude

1 answer

If you read the reference page for GetTouchingParts(), you'll see that if the "part itself has CanCollide set to false, then this function will return an empty table unless it has a TouchInterest." In order to insert a to fix your problem, you can temporarily connect a listener to the part's Touched event, call GetTouchingParts(), and then call Disconnect() on the RBXScriptConnection (as highlighted here).

script.Parent.ClickDetector.MouseClick:Connect(function(plr)
	local connection = script.Parent.Touched:Connect(function() end)
	local gett = script.Parent:GetTouchingParts()
	connection:Disconnect()
 	for i,v in pairs(gett) do
		print(v.Name)
	end
end)

If you ever need to permanently connect an event listener to the Touched event, remove this fix because it will interfere with the actual event listener.

This discussion is preserved as it was. New answers, comments, and votes are closed.