Detect if a guiobject is inside a frame?

How to detect if a guiobject or is inside a frame specifically? Tried checking devforum post but I don’t seem to find it helpful and working.

1 Like

What do you mean by touches? Could you explain what you want to achieve in more detail?

I mean it by colliding, but nevermind I only want to detect if it’s inside the frame.

Then you could do

local GUIobject = script.Parent
if GUIobject.Parent.Name == "YOURFRAMENAME" then
      -- Whatever code you want to run
end

I don’t meant as parent, i mean like inside an object.

What do you mean by “Inside”? The only way to tell if say a textbutton is “Inside” of a frame, is to check if its parent is that Frame. If you could explain what you mean by inside as I feel like I’m not understanding what you mean, it would be great.

When dealing with a situation like this, I would recommend using AbsoluteSize and AbsolutePosition. In your case, I will assume you have an AnchorPoint of 0, 0 (if you want, it can be different, but you will have to edit the math). So we need to find the top left corner position and the bottom right in order to compare that to the edges of your container frame. Both of these values are Vector2s.
The Absolute Size is already at the top left, so:

local topLeftPos = guiObject.AbsoluteSize

And for the bottom right, we can just add the absolute size to offset it

local bottomRightPos = topLeftPos + guiObject.AbsoluteSize

Great! So now we know the ‘bounds’ of our gui object. We need to repeat this process with the containing frame.

local frameTopLeft = frame.AbsolutePosition
local frameBottomRight = frameTopLeft + frame.AbsoluteSize

Now we can define a function that will compare the bounds and make sure the gui object is within them.

local function isInBounds()
  if topLeftPos.X > frameTopLeft.X or topLeftPos.Y > frameTopLeft.Y
    and bottomRightPos.X < frameBottomRight.X or bottomRightPos.Y < frameBottomRight.Y
  then
      return true
  else
      return false
  end
end 

This should allow you to determine if the frame is within the bounds. Btw, I have not tested this code, so make sure to check for syntax errors, but the concepts should be right. Hope this helps!

3 Likes

Alright i’l try that but i’m gonna go to bed before trying this, i’ll leave as a solution when I’m back.

Note: this is meant to check if it is COMPLETELY inside of the frame. I’m not sure if that is the behavior you want, but the math could work the other way around. Just play with the less than/greater than signs a bit for your intended Behavior.

1 Like

I want to check if it’s also partly inside of the frame.

Ok, so I changed the and statements to or statements. That should work

2 Likes

You can use this script i found :

function collidesWith(gui1, gui2)  -----some parameter you can put your GUI1 and GUI2 in 
    local gui1_topLeft = gui1.AbsolutePosition
    local gui1_bottomRight = gui1_topLeft + gui1.AbsoluteSize

    local gui2_topLeft = gui2.AbsolutePosition
    local gui2_bottomRight = gui2_topLeft + gui2.AbsoluteSize

    return ((gui1_topLeft.x < gui2_bottomRight.x and gui1_bottomRight.x > gui2_topLeft.x) and (gui1_topLeft.y < gui2_bottomRight.y and gui1_bottomRight.y > gui2_topLeft.y))   --- return their values
end

its more easy to use as you just have to put your GUI in the parameter
also it detect if its overlap

1 Like

You just do aabb collision but check if all verticies are colliding on a point based method. Simple stuff really.

What is bottomLeftPos and frameBottomRight defined as?

My bad, I mixed up bottomLeftPos, it should actually be bottomRightPos. Sorry for the confusion (I edited the original post so it makes sense now :sweat_smile:)

1 Like

What about the frameBottomRight? Is it frameBottomLeft?

Sorry, I just changed the definition of frameBottomLeft to frameBottomRight up above where I defined the local variables. It actually is the bottom left corner of the frame, so I wanted the name to reflect that, but I screwed up spectacularly.

1 Like

I don’t think it’s working, did you try it?

Edit: Wait I did an oops hold on

Yeah It detected true if a guiobject is also outside a frame, did you test it?