Detect If GUI frame is colliding with another


After the line of code, I’m really confused on why it’s saying there not colliding

If anyone knows any soloution please reply!

Have to tried moving it around and getting a true when they’re not colliding?

“local gui1_topLeft = gui1.AbsoluteSize” this seems wrong no? Shouldnt it be position - absolutesize/2? and then the bottom right would be pos + half of absolutesize

I have no idea what your saying, can you make it so my monkey brain understands what your saying

If a frame is 200x50 pixels then absolute size is x=200 and y=50 but this has nothing to do with position only size, so making a variable named topleft and bottomright based on the size of the frame seems wrong. Instead of you take the position you get the center of the frame, say the center is x=960 and y=540 then topleft would be 960(center position)-100(half of its size as we wanna go from the center to the left).

TLDR, you’re telling the function that top left is not where top left actually is and the formula should be “position.X-size.X.Offset/2” and “position.Y-size.Y.Offset/2”.
You can test this by moving the boxes randomly around, especially somewhere close to the top left of your screen, and the collision should say true even when they are nowhere close to each other

I have legit no idea what your saying. Sorry If I’m sounding rude, I’ll just uh send my current code

local cowbell = script.Parent["Rhythm Heaven Cowbell"]
local rep = game:GetService("ReplicatedStorage")
local localp = game:FindFirstChild("Players").LocalPlayer
local gui1 = script.Parent.Parent.Hitbox
local gui2 = script.Parent.Parent.pot
local debounce = false
function collidesWith(gui1, gui2)  -----got this from dev fourm thanks to whoever made it
	local gui1_topLeft = gui1.AbsoluteSize
	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
script.Parent.MouseButton1Click:Connect(function(plr)
	if not debounce then
		if start == true then
			start = false
			script.Parent.Parent.TextLabel.Text = "Tap Joe In Time with the rhythm."
			cowbell:Play()
			wait(1)
			cowbell:Play()
			wait(1)
			cowbell:Play()
			wait(0.5)
			cowbell:Play()
			wait(0.5)
			cowbell:Play()
			wait(0.5)
			cowbell:Play()
			script.Parent.Parent.TextLabel.Visible = false
			wait(0.5)
			script.Parent.Parent.pleasedontban:Play()
		else
			script.Parent.Whoosh:Play()
			script.Parent.Image = "rbxassetid://121803704513390"
			wait(0.13)
			print(collidesWith(gui1, gui2))
			if collidesWith(gui1, gui2) then
				print("success!")
			end
			script.Parent.Image = "rbxassetid://126828287438911"
		end
	end
end)

Since you made a comment thay you got it from the dev forum I’d recommend seeing if you can find the original, that could probably help a lot with fixing it. Otherwise I could try to have a look when I get on PC.

Here,

Seems like you just have a little typo with setting the gui1 to absolute size and gui2 to absolute position, see if setting gui1 to absolute position (same as gui2) works

1 Like

TYSM! It works! Thank you, really!

nvm im stupid, I made the Gui2 pos as the same as Gui1

It still doesn’t work :confused: How can I fix this?

Your typo is because you didn’t copy paste the code from the dev forum which would’ve fixed that issue for you at any point. I don’t say this to be mean, just I find it a bit unnecessary to not ask AI as even AI had no problems finding the solution right away and could explain where your typo came from etc. Like it or hate it, AI is a very useful tool in some cases. I checked if it could find the issue which it did and complain about your start variable which I personally have no idea what is for.
Here’s what I got ChatGPT - Collisions in GUI Fix you should also look at tip 2 and 3 as the debounce is redundant since it’s not used and wait() is deprecated.
Here’s the code that should work (I don’t have the UI stuff to test it)

local cowbell = script.Parent["Rhythm Heaven Cowbell"]
local rep = game:GetService("ReplicatedStorage")
local localp = game:FindFirstChild("Players").LocalPlayer
local gui1 = script.Parent.Parent.Hitbox
local gui2 = script.Parent.Parent.pot
local debounce = false
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
script.Parent.MouseButton1Click:Connect(function(plr)
	if not debounce then
		if start == true then
			start = false
			script.Parent.Parent.TextLabel.Text = "Tap Joe In Time with the rhythm."
			cowbell:Play()
			wait(1)
			cowbell:Play()
			wait(1)
			cowbell:Play()
			wait(0.5)
			cowbell:Play()
			wait(0.5)
			cowbell:Play()
			wait(0.5)
			cowbell:Play()
			script.Parent.Parent.TextLabel.Visible = false
			wait(0.5)
			script.Parent.Parent.pleasedontban:Play()
		else
			script.Parent.Whoosh:Play()
			script.Parent.Image = "rbxassetid://121803704513390"
			wait(0.13)
			print(collidesWith(gui1, gui2))
			if collidesWith(gui1, gui2) then
				print("success!")
			end
			script.Parent.Image = "rbxassetid://126828287438911"
		end
	end
end)

Im not saying AI always does the job, as Leo AI in brave was smoking something, and the builtin Assistant in Studio seems to have a mood which chooses how good it’s help is. But chatgpt usually helps most problems.
If the there’s still a problem and AI can’t fix it, I’ll be happy to still help where I can

1 Like