Detect If GUI frame is colliding with another

I’m trying to make a rhythm sorts game and I’m trying to make it so if the pot is in the hitbox, it gets flung back.

This is my current code

local start = true
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
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)
			if collidesWith() then
				gui2:TweenPosition(UDim2.new(0.37, 0,0.403, 0), "Linear", "In", 1, false)
			end
			script.Parent.Image = "rbxassetid://126828287438911"
		end
	end
end)


The transparent black square is the hitbox

1 Like

Please search a little about your topic before posting. Also wait() is deprecated, use task.wait() instead.

1 Like

Yes, I’ve done my research but I keep getting an error.

What’s one the line that errors?


From the function, I don’t know why its causing this error

Try printing the gui1, gui2 both under where you define them and under the collidesWith() function then see what they print.

print(gui1, gui2)
function collidesWith(gui1, gui2)  -----got this from dev fourm thanks to whoever made it
	print(gui1, gui2)
	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

Make sure you are pathing these correctly:

local gui1 = script.Parent.Parent.Hitbox
local gui2 = script.Parent.Parent.pot

I’m a bit confused on what your saying… Like put the print under the place where I defined gui1 and gui2?

Your not sending data to the function, it errors because you need to send parameters

1 Like

Should I define them inside the function?

Ok, so basically, you already have gui1 and gui2, but when that function is called, you overwrite those names with the function parameters, and since you don’t send any parameters, gui1 and gui2 is nil

Still confusing, but how can I send the parameters to the function?

You can either remove the parameters from CollidesWith, or do this: local result = CollidesWith(gui1, gui2)

Sorry If I sound like a total idiot but, where do I place the result? I’m not that good with code.


local start = true
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
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)
			if collidesWith(gui1, gui2) then
				gui2:TweenPosition(UDim2.new(0.37, 0,0.403, 0), "Linear", "In", 1, false)
			end
			script.Parent.Image = "rbxassetid://126828287438911"
		end
	end
end)

Ok, I’ll try adding a print so I can check if it works or not, I’ll update you later

The code is working fine, no errors but I believe it’s due to how I originally coded this section.
image
There is nothing in the output, can you help me fix it?

Well that must mean they are not colliding then, or looking at your code, it might just be checking the corners (actually no)

Add above that line print(CollidesWith(gui1, gui2))

1 Like


White is the pot,
Black is the hitbox