Why does the click work even though the button is under a frame?

Hello!

Untitled

When I click on the frame, button MouseButton1Down event fires despite the fact that the button appears to be under the frame.
How could I fix that?
(Button ZIndex = -10, Frame ZIndex = 10)

script.Parent:WaitForChild("button").MouseButton1Down:Connect(function()
	print("Clicked")
end)

Thats it, thanks!

1 Like

I can’t test this out myself at the moment, but I imagine an easy fix would be to add a condition within the MouseButton1Down event to check whether the frame is active or not. If it is, yield, if it’s not, clear the condition.


Example:

script.Parent:WaitForChild("button").MouseButton1Down:Connect(function()
	if frameThatIsCoveringTheButton.Visible then
		--//print("Frame is open, ignore click.")
	else
		--//print("Clicked.)
	end
end)

This may be the hacky solution around it, but again, I can not access Studio for testing at the moment.

enable the active property of the frame

You could try this Guide: Positioning and Sizing UI Objects | Documentation - Roblox Creator Hub

It is enabled already (aaaaaa)

Did you set the property ClipDescendants on the frame to true ?

Yeah I have set it. It didn’t worked.

Unfortunately, you’ll have to check for overlapping GUIs yourself. If you switch to using the Activated event instead of MouseButton1Down, you’ll get access to an InputObject your callback can use to extract the mouse position of the click and loop through PlayerGui’s GetGuiObjectsAtPosition returned array to check if any objects with Active set to true overlap the button. I believe MouseButton1Down also provides mouse coordinates, but I trust Activated and InputObjects more than the MouseButton events.

local playerGui = localPlayer.PlayerGui
local textButton = script.Parent:WaitForChild("button")

local function isOverlapped(self: GuiObject, inputObject: InputObject)
	local intersectingGuiObjects = playerGui:GetGuiObjectsAtPosition(inputObject.Position.X, inputObject.Position.Y)
	for _, guiObject: GuiObject in pairs(intersectingGuiObjects) do
		if guiObject == self then  -- found the button itself with no overlap
			return false
		end
		if guiObject.Active then -- object above the button is Active, overlapping
			return true
		end
	end
	return false
end

textButton.Activated:Connect(function(inputObject: InputObject)
	if isOverlapped(textButton, inputObject) then
		return -- button is overlapped, ignore activation
	end
end)

If you want all GUI objects to overlap and block the button, you can remove the if statement in isOverlapped that checks .Active and just return true for any GuiObject above the button.

1 Like

I just tested it and putting an invisible button inside the frame with size of 1,0,1,0 and a higher zindex than the button you want to cover results in what you want to happen

Yeah, you’re right. MouseButton1Down definitely gives inaccurate coordinates.

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