Cloned SurfaceGui frame is only detecting collisions with one other frame, not all

Hi. This has been killing me for about 2-3 hours. I’m stuck and I shouldn’t be, this shouldn’t be this hard to figure out but it is being that way.

All I want is for all existing “notes” to be accounted for when checking if the new note created can be placed at the mouse’s location. I do not want them overlapping.

Here is part of the local script that may need to be read to get a full understanding. The local script is parented under the “Create Note” button.

local DragStatus = false;
local CanPlaceNote = true;
local Note = nil;
local Wall = game.Workspace:FindFirstChild("Wall");
local ClientScreen = Player.PlayerGui:FindFirstChild("Screen");

function CheckOverlap(Frame1, Frame2)
	local F1_TopLeft,F1_BottomRight = Frame1.AbsolutePosition, Frame1.AbsolutePosition + Frame1.AbsoluteSize
	local F2_TopLeft,F2_BottomRight = Frame2.AbsolutePosition, Frame2.AbsolutePosition + Frame2.AbsoluteSize
	return ((F1_TopLeft.X < F2_BottomRight.X and F1_BottomRight.X > F2_TopLeft.X) and (F1_TopLeft.Y < F2_BottomRight.Y and F1_BottomRight.Y > F2_TopLeft.Y))
end

script.Parent.MouseButton1Click:Connect(function()
	local MessageBox = script.Parent.Parent:FindFirstChild("Message");
	if MessageBox.Text ~= "" then
		print(#Wall.Screen:GetChildren())
		local NewNote = Items:FindFirstChild("Note"):Clone();
		NewNote.Name = "Note"
		NewNote.Parent = Player.PlayerGui:FindFirstChild("Screen").BG
		NewNote.ZIndex = -1
		NewNote:SetAttribute("Author", Player.Name)
		
		NewNote:FindFirstChild("UIStroke").Transparency = 0
		
		NewNote:FindFirstChild("Message").Text = MessageBox.Text
		NewNote:FindFirstChild("Author").Text = NewNote:GetAttribute("Author")
		Note = NewNote
		
		if not DragStatus then
			DragStatus = true
		end
	end
end)

ClientScreen.BG.MouseMoved:Connect(function(X, Y)
	if DragStatus then
		for i,v in pairs(Wall:FindFirstChild("Screen").BG:GetChildren()) do -- This is where I see the real issue. It is only checking for one note under "Screen" and not all.
			if CheckOverlap(Note, v) then
				Note:FindFirstChild("UIStroke").Color = Color3.fromRGB(255, 24, 24)
				CanPlaceNote = false
			else
				Note:FindFirstChild("UIStroke").Color = Color3.fromRGB(96, 255, 47)
				CanPlaceNote = true
			end
		end
		Note.Position = UDim2.new(0, X - Note.Size.X.Offset/2, 0, Y - Note.Size.Y.Offset/2)
	end
end)

PlayerMouse.Button1Down:Connect(function()
	if DragStatus then
		if CanPlaceNote then
			RemoteEvents:FindFirstChild("PlaceNote"):FireServer(Note.Message.Text, Note:GetAttribute("Author"), Note.Position) -- The client-side note is deleted and a note is cloned with the same details on the server.
			Note:Destroy()
			DragStatus = false
			Note = nil
		end
	end
end)```

I've tried everything I could possibly think of at the moment, but nothing has fixed this. It's confusing the heck out of me. 

Any help is greatly appreciated, thank you.
1 Like

I got it. After like 3-4 hours now.

I changed the function to return true or false depending on if there is a “true” value found in the temporary table.

local Temp = {}
	for i,v in pairs(Wall:FindFirstChild("Screen").BG:GetChildren()) do
		local F1_TopLeft,F1_BottomRight = Frame1.AbsolutePosition, Frame1.AbsolutePosition + Frame1.AbsoluteSize
		local F2_TopLeft,F2_BottomRight = v.AbsolutePosition, v.AbsolutePosition + v.AbsoluteSize
		table.insert(Temp, ((F1_TopLeft.X < F2_BottomRight.X and F1_BottomRight.X > F2_TopLeft.X) and (F1_TopLeft.Y < F2_BottomRight.Y and F1_BottomRight.Y > F2_TopLeft.Y)))
		print(Temp)
	end
	
	if table.find(Temp, true) then
		return true
	else 
		return false
end

Just please let me know if there is a better/more smarter way of doing this.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.