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

How to see if a Model is touching another model?

I am trying to make a placement system for my game, and I want it so if you try to place a model inside an already built model, it will tell you. I have already tried adding a HitBox to the model and making a Touched function with it, but it did not fire when it touched the other model. Am I doing it wrong or is there a special way to do it?

Edit: Here is the script I tried

local HitBox = script.Parent

HitBox.Touched:Connect(function(Part)
	if Part.Name == "HitBox" then
		print("Hit the HitBox")
	end
end)

2 comments

0

Mind sharing your attempt?

0

:GetTouchingParts()

1 answer

✓ Accepted answer

To see if a models' parts are intersecting with anything, here's what you could do:

  1. Get all parts in the model, then call :GetTouchingParts(). This will return a table with all the parts that are intersecting with that part.
  2. Loop through the table, and only check if there are parts that don't belong to your model. This eliminates parts inside other parts that are part of the model, since we're checking if the model is colliding with the environment.
  3. If there are parts in the table that are not descendants of your model, then yes, you're colliding with something. This is where you make your model turn red or something to indicate that you can't place there.

If you have a hitbox part for the model, though, you could just do step 2 only for the hitbox instead of every part.

Also, here's a bug to keep in mind:

  1. :GetTouchingParts() doesn't really work as expected with walkthroughable (CanCollide = false) parts, it would just return an empty table each time. The solution to this is connect to the part's Touched event prior to calling :GetTouchingParts(), then disconnect from the event; this makes it return what parts are intersecting as expected. I learned this from here.

I recommend reading this SH guide by EgoMoose since that seems to be what you're going for. Don't get intimidated by the first part of the post though

Hope that helps!

3 comments

0

already said that

1

Thanks! I have been wondering how people have been able to do it. You really helped!

0

You're welcome!

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