How would I detect if something is inside of another part?

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to detect if something is inside of a part and if so then it needs to print (“Something is inside of the part”).

  2. What is the issue? Include screenshots / videos if possible!
    I don’t know much about :GetPartsInBox() or anything like that.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried YT and Devforum.

1 Like

You want to use the GetTouchingParts GetPartsInPart method.

For example:
Lets say you have a part called ‘Box’ and you want to determine if any other parts are touching it.

Calling game.Workspace:GetPartsInPart(Box) Box:GetTouchingParts() will return a table of all parts that are touching ‘Box’, if the table is empty that means there are no touching parts, if the table is not empty, then there are parts touching it.

You can condense all of it into this simple expression:
#workspace:GetPartsInPart(Box) > 0 #Box:GetTouchingParts() > 0
This will return a boolean indicating whether or not the part is inside of another.

1 Like

Hello, thanks for responding, but is it possible if you could give me an example script of how this would work?

GetTouchingParts won’t work if the part has CanCollide off. If that is the case then you will need to use game.Workspace:GetPartsInPart() to do that:

while true do
	wait()
	if #game.Workspace:GetPartsInPart(part) > 0 then
print("Something is inside of the part")
end
end
4 Likes

Ok thanks! i will try this on my code as soon as possible, btw does this also detect terrain?

Sure!
Here is an example script:

local Box = workspace:FindFirstChild("Box") --This is the part which we will be checking for intersections

local isTouchingOtherParts = #Box:GetTouchingParts > 0 --This returns a boolean (true or false) that indicates whether or not Box is intersecting other parts

if isTouchingOtherParts then
-- do stuff
end
2 Likes

Thanks! I will try this code aswell, btw does this detect terrain? If not how can I make it detect terrain?

Yes, this is the method you want to use, not the one I linked, excuse my mistake.

@RetroAmythest
Afaik this will not detect terrain. If you need to detect terrain, there are a couple ways you can go about it.
Off the top of my head, can use raycasts, shapecasts, or region3.
I would probably recommend using a shape cast.

1 Like