Better way to detect gui collisions

What I’m trying to achieve is basically just UI Collision, in other words, to detect if a gui is touching a certain gui and if it is, move the gui thats being touched to the left and if the left is occupied moving another gui being touched to the right. My method is not very ideal.

local PlayerGui = game:GetService("Players").LocalPlayer.PlayerGui
local UIs = {}
function Reposition(ImageLabel,r)
	if r then -- 1 goes to left
		TweenService:Create(ImageLabel, TweenInfo.new(0.5), {Position = ImageLabel.Position - UDim2.new(0.2,0,0,0)}):Play()
	else
		TweenService:Create(ImageLabel, TweenInfo.new(0.5), {Position = ImageLabel.Position + UDim2.new(0.2,0,0,0)}):Play()
	end
end
function CheckPosition()
	if PlayerGui.Pocketer:FindFirstChild("Killa1") then
		Reposition(PlayerGui.Pocketer.Killa1.Frame, true)
		return UDim2.new(0.475, 0,0.921, 0) 
	end
	if PlayerGui.Pocketer:FindFirstChild("Killa2") then
		Reposition(PlayerGui.Pocketer.Killa2.Frame, false)
		return UDim2.new(0.475, 0,0.921, 0) 
	end
	return UDim2.new(0.475, 0,0.921, 0)

end
function CreateGui()
	local GUI = game:GetService("ReplicatedStorage").ScreenGui:Clone()
	GUI.Frame.Position = CheckPosition()
	GUI.Name = "Killa"..#UIs
	GUI.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Pocketer
	TweenService:Create(GUI.Frame, TweenInfo.new(0.4), {Size = UDim2.new(0, 255,0, 100)}):Play()
	table.insert(UIs, #PlayerGui.Pocketer:GetChildren())
	task.delay(4,function()
		GUI:Destroy()
		table.remove(UIs, #PlayerGui.Pocketer:GetChildren())
		print(#UIs)
	end)
	GUI.Destroying:Once(function()
		UIs[GUI.Frame] = nil --ignore.
		print(#UIs) 
	end)
end

Maybe this might help you; I personally haven’t used it though.

1 Like

That whole module is utter confusion, especially the hitter and collider, would prefer a module without tables. All what I’m trying to do is detect which gui another gui is touching then return the touched gui.
Besides, it DOES NOT LIKE IT when the frame gets destroyed.

Edit: Managed to figure it out myself from a different, now to figure out if left is occupied or not.

local function areFramesOverlapping(myImageLabel)
	if not myImageLabel then 
		return
	end
	local guisDetectedAtPosition = PlayerGui:GetGuiObjectsAtPosition(myImageLabel.AbsolutePosition.X, myImageLabel.AbsolutePosition.Y)

	for _, v in guisDetectedAtPosition do
		return true,v
	end

	return false -- return false if nothing is detected
end
function CreateGui()
	local GUI = game:GetService("ReplicatedStorage").ScreenGui:Clone()
	GUI.Name = "Death"
	GUI.Parent = game:GetService("Players").LocalPlayer.PlayerGui.Pocketer
	TweenService:Create(GUI.Frame, TweenInfo.new(0.4), {Size = UDim2.new(0, 255,0, 100)}):Play()
	local bool,instance = areFramesOverlapping(GUI.Frame)
	if bool then
		TweenService:Create(instance.Parent, TweenInfo.new(0.5), {Position = UDim2.new(0.275,0,0.921,0)}):Play()
	end
end
1 Like

Ignore. I fixed everything.

1 Like