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)
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.
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.
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