How to detect click outside SurfaceGui

How could I detect when the user clicks outside a specific gui object after clicking on said object?

Yield for InputBegan on UserInputService and check whether it was a click or not. Alternatively, use ContextActionService to ‘monitor’ the clicking.

1 Like

local UserInputService = game:GetService("UserInputService")

local function onInputBegan(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		print("The left mouse button has been pressed!")
         elseif nput.UserInputType == Enum.UserInputType.MouseButton2 then
		print("Right click"!)

OR, Make a huge TextButton on the Surface.

OR, Use a ScreenGui.

1 Like

you forgot to close the string


i’m stupid lol, didn’t test bc i knew it would work

1 Like

I made the textbutton, but what would be the easiest way to detect when another is clicked?

Simply with MouseButton1Click or you can use UserInputService + :GetGuiObjectsAtPosition():

local PlayerGui = game:GetService("Players").LocalPlayer:WaitForChild("PlayerGui")
local UIS = game:GetService("UserInputService")

	if not Chat and Input.UserInputType == Enum.UserInputType.MouseButton1 then
		local Vect2 = Input.Position
		local Table = PlayerGui:GetGuiObjectsAtPosition(Vect2.X,Vect2.Y)

In that table all the objects that that vector touches will appear.

or with InputBegan on the object.

1 Like

you should have the elseif on the same indentation level as the if to make it more readable.

also you forgot to type an i in nput.UserInputType, and the ! is outside the string on print("Right click"!)


Thanks, that worked, I just added an aditional if statement to check if they’re the same object.

1 Like

i’m just not having it today LOL