Find percentage how much a guiobject is inside a frame

I’m trying to find the percentage of how much a GUI object is inside a frame. For example, if the subject is exactly inside a frame then, it will be 100%. If it’s halfway inside then it would be 50%. Size and Positions also matter too. I’ll be so much thankful if anyone can help me.

Detection Script:

function DetectIfInside(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
1 Like

I found an answer to this question on stack overflow if you want to check it out

Drawing the rectangles by hand can help you understand the understand and visualise the problem

Only problem is I don’t know how to convert it and don’t understand some of it.

thx for using my script anyway i got lot of message from people telling me how to find percentage etc. what i know far is that you must substract gui1 and gui2 position then divide it by the position of gui1 and then times it by 100 here is the formula Percentage Change - Percentage Increase and Decrease | SkillsYouNeed or you can work out the area also i recomend what @crimcat said

1 Like

Well no problem. Anyway about what’re you saying, I know the cause of the bug that I’m having right now but I don’t know how to multiply it by a 100 as it says “attempt to perform arithmetic (div) on UDim2”

local main = script.Parent.Frame
local overlapper = script.Parent.overlapper

local subract = main.Position - overlapper.Position
local divide = subract / main.Position
local answer = divide * 100

print(answer)

Also i’m not doing percentage changes, I only want to know the percentage of overlapping.

i am very sorry but The Huge problem here is that for now you cant divide Udim2 and vector2 Allow arithmetic for certain objects that currently don't allow it

So what now if I can’t fix this? Are there any other methods or smth?

1 Like

What i know is that you can only plus or substract it div and mul isnt suported with the help of a for i loops you can basicaly do a repeated subtraction which is division but with substraction What is Repeated Subtraction? Definition, Facts, Method, Examples.

1 Like

What do I repeat subtract (divide) and what do I convert it to?

I’m still looking for help btw.

Still looking for help guys, I’ll be glad if anyone can answer.

The absolute position of it can be interpretted as the position. So say we got the scale, say 0.5 on the x and 0.6 on the y then the position is simply that * the viewport size

Vector2.new(0.5*workspace.CurrentCamera.ViewportSize.X, 0.6*workspace.CurrentCamera.ViewportSize.Y)

It should be noted also that you already have this value if its like a frame, its just frame.AbsolutePosition.

x1 = Vector2.new(frame.AbsolutePosition.X, frame.AbsolutePosition.Y)
x2 = Vector2.new(frame.AbsolutePosition.X + frame.AbsoluteSize.X, frame.AbsolutePosition.Y)
x3 = Vector2.new(frame.AbsolutePosition.X, frame.AbsolutePosition.Y + frame.AbsoluteSize.Y)
x4 = Vector2.new(frame.AbsolutePosition.X+ frame.AbsoluteSize.X, frame.AbsolutePosition.Y + frame.AbsoluteSize.Y