Prevent mouse click passing through GUI


So the way the Building System works is:

When the players taps somewhere on the screen within the plot, the object goes there and if they want to confirm placement, they click on the green button "Place"

However when they click the Place button it looks like they also tap within the 3D workspace so the object goes there instead, wondering how to disable that?

			connection2 = game:GetService("UserInputService").InputBegan:Connect(function(input,gp)
				if gp then return end
                -- computer building version here



                -- mobile placement:
				BuildingGui.Parent.Place.MouseButton1Click:Connect(function()
                    -- code irrelevant to the problem (to place the object)
				end)
			end)
1 Like

try turning the buttons modal = false when building

Modal on the buttons is already false.

Try this maybe Know whatever GUI the Mouse is hovering - #2 by wevetments

If there is a GUI detected, then don’t place the object

That won’t work for mobile. Hovering on mobile is not going to work out.

It doesn’t use hovering, it just checks which GUI is under a position, give it the 2D position where the player touched/clicked

mouse.Move:Connect(function()
	local GUIs = game:GetService("StarterGui"):GetGuiObjectsAtPosition(mouse.Position.X, mouse.Position.Y-36)
end)

Position is not a valid member of PlayerMouse "Instance"

That code doesn’t work?

It’s Mouse.X and not Mouse.Position.X, do the same for Y too

It doesn’t work properly on mobile as on mobile, you need to tap, there is no hovering, here you need to kinda hold the button for the function to be ran and be recognised, plus it returns a table with no objects in it.

Is there no function to disable input or interaction within the 3D workspace directly when clicking on a UI?

You got my point wrong, you run game:GetService("StarterGui"):GetGuiObjectsAtPosition() WHEN the user clicks, on the position where he clicked (Screen Position)

It returns with a table with no instances in it even though I am hovering on mouse and keyboard.

Pretty sure I am overthinking this, all I need is to prevent mouse click passing through GUI. Anyway to do exactly that?

Maybe this? Prevent mouse click going through GUI controls - #5 by Aquaventurer

Same results. I was also thinking that would fix it.

https://developer.roblox.com/en-us/api-reference/property/GuiObject/Active

Turn this on for a container frame (something that ideally encompasses the whole bottom toolbar, such as your scrollbars), it’ll sink input and won’t allow any interaction with the 3D world. More explanations available on the documentation page.

Just tried and no results from that. I even made a frame that covers the whole screen and made it Active and I couldn’t move my character but I could move the object still, could things be overriding because of my placement script?

1 Like

I’m not exactly sure how you’re doing your placement because you’ve taken out that entire portion of code (either the code itself or a very, very detailed explanation of how you accomplish object placement would be appreciated), but that doesn’t sound right?

I just made a quick repro in Studio with an active frame covering the entire screen and a script that prints the local mouse’s target when MouseButton1 input is sent and it won’t interact with the 3D world. My case works but yours supposedly doesn’t. Either you need to check you properly set Active on these objects or you need to provide more detail.

The following script does not print when you click:

local UserInputService = game:GetService("UserInputService")
local LocalMouse = game:GetService("Players").LocalPlayer:GetMouse()

UserInputService.InputBegan:Connect(function (inputObject, gameProcessedEvent)
	if gameProcessedEvent then return end
	
	if inputObject.UserInputType == Enum.UserInputType.MouseButton1 then
		print("Clicked")
		print(LocalMouse.Target)
	end
end)

Gui consists of a single stretched frame across the viewport with Active on:

image

Repro file:

Gui Sink Mouse Repro.rbxl (23.4 KB)

3 Likes

Okay, I see where the problem is, so I have two things:

-- Snap function basically makes object go to the mouses position
--and its the formula for snapping objects to the grid.
--But I am running a .RenderStepped event, thus when clicking UI it still clicks
--in the 3D world I am guessing

connection = game:GetService("RunService").RenderStepped:Connect(function()
	if obj ~= "Path" then
		Snap(clone,workspace.Plots[plotName].SizeBlock,4)
	else
		Snap(clone,workspace.Plots[plotName].SizeBlock,8)
	end
	mouse.Icon = "rbxassetid://6373607089"
end)

connection2 = game:GetService("UserInputService").InputBegan:Connect(function(input,gp)
	if gp then return end
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not touching then
        -- code to place object
	end
end)

So we need to switch the RenderStepped event to be a InputBegan event I believe?

Edit: Tested it, works with InputBegan but I’d always like the objects position to be updated, how will I do that?

It would probably be wiser at this point to separate the ways you code movement and placement between computer devices and mobile then so you can avoid this issue. Most building games, even off platform, control very differently on mobile than they do on computer devices in light of things like this.

For a computer device, it will have no problem using a mouse in order to determine the grid in which an object should be placed on. You can also just click and interact with the object or place it down. For mobile, the typical workflow is that the object’s movement is only changed when holding down on the screen and then there are on-screen buttons to place, cancel and whatever other actions you need.

Consider using TouchMoved to determine when the user moves their finger on the screen and then ViewportPointToRay to convert the 2D positions of their touch to a 3D position - this will help you find the grid that you need to snap to. From there, it should interfere minimally with your placement Guis. You might need to accommodate for this in your RunService connection* so that the object doesn’t snap to your finger’s location.

* I recommend using Heartbeat over RenderStepped. RenderStepped is something you should typically use scarcely, like for camera and character transparency updates.

1 Like