Detecting object in another object

Hello,
Long story short, i need to detect if object 1 (red ball) is in object 2. (grey ball)

i tried to use Region3 but it does not work for spheres…

Is there any way to detect if objects are in each other?

Yes, with the parent.

local Object1 -- Red Ball
local Object2 -- Grey Ball

if Object1.Parent == Object2 then
   return true
else
   print(Object1.Parent.Name .." is the parent's name.")
   return false
end

If that doesn’t work, just do this.

local Object1
local Name -- Put the name of the second object

if Object1.Parent.Name == Name then
   return true
else
   print(Object1.Parent.Name .." is the parent's name.")
   return false
end

Oh, did you mean with parent/child or with the position?

They are both in workspace, i want to detect if they are in eachother and when they are.
EDIT: this code up can be usefull, thank you i will take it.

i think you can achieve this using collision groups. Collision Filtering | Documentation - Roblox Creator Hub

You can use touched and a boolvalue for that.

Like this;

local Bool
local Object1
local Name -- Second Object's Name

Object1.Touched:Connect(function(Hit)
   if Hit.Name == Name then
      Bool.Value = true
   end
end)
1 Like

You could check if the red ball is located in some radius from the center of the grey ball.
Please mark this post as a solution if this helped :slight_smile:

1 Like

You can use :GetTouchingParts():

local Object1
local Object2
for i,v in pairs(Object1:GetTouchingParts()) do 
    if v = Object2 then
        --Code here
    end
end
2 Likes

I think that can work but i think it would take too much procesing power, if i run out of ideas that can help.

1 Like

i will test it, give me minute

1 Like

Adding onto that, I think he means use magnitude. you can use:

local redBall = game.Workspace.RedBall

local greyBall = game.Workspace.greyBall

local distance = (redBall.Position - greyBall.Position).magnitude

if distance == 0 then

-- code

end

Magnitude checks the distance between 2 CFrames. This script only checks once, so you’ll have to use a loop of some kind. Note, if you dont know what magnitude is, it’s basically the distance between 2 cframes. I used an if to detect if the distance was 0, meaning it was in the center, so you should probably do if distance <= 5. 5 is how many studs away from the center the object is

3 Likes

i get this error
12:06:46.341 GetTouchingParts is not a valid member of Model “Workspace.Object1” - Server - Script:3

1 Like

Wait Object1 is a model?
I thought it was a basepart.

1 Like

it is basepart, but it says its model for some reason

1 Like

local greyBall = game.Workspace.greyBall

local distance = (redBall.Position - greyBall.Position).magnitude

if distance => 0 and distance < greyBallSize``` 

-- code

end
3 Likes

Can you send a screenshot of the object explorer?

1 Like

my bad, there was model in workspace called Object1
Now all works, thank you

2 Likes

ok, thank you evryone, i will take all your ideas and make 2 stage detection system with GetTouchingParts and magnitude calculations.
Cheers from Serbia!

3 Likes