Detect part touching another part

Hey! I want to make my model’s rocks’ material property change to the other part’s material when they are in contact. My script doesn’t work though, it only works when the rock gets touched by the player.
How do i check if a part is touching another, i can’t use :GetTouchingParts since my parts are uncollideable.

local rocks = game.Workspace:WaitForChild(Rocks) -- model

	for i, v in pairs(rocks:GetChildren()) do
		v.Touched:Connect(function(hit)
			if v.Name == "Rock" then
				v.Material = hit.Material
			end
		end)
	end
	

Thanks for helping me out!

Right now your code basically gets all children of the rocks model and then checks if the child of the model is named “Rock”.
I’m not sure if you want that to happen or if you want to check if the “hit” / the part that touches the child of the model is named rock. In that case I would change your script to:

if hit.Name == "Rock" then
   v.Material = hit.Material
end

Hopefully I understood correctly and this helps you, if not please correct me!

1 Like

https://developer.roblox.com/en-us/api-reference/function/WorldRoot/GetPartsInPart

Because you mentioned “CanCollide” is disabled for both parts.

1 Like

You can actually use GetTouchingParts even if the parts have cancollide set to false you just need to give them a touched signal

local Empty = function() end
Rock.Touched:Connect(Empty)

Then you will be able to use GetTouchingParts()


Source: BasePart:GetTouchingParts

Although using GetPartsInPart will also be fine

1 Like