How to know if a player clicks a button, or clicks somewhere else that's not a button?

So I need to know if when the player clicks, if it’s ScreenGui.Button1, or something else. I can’t do mouse positions because that button always moves. I don’t even know where to start. Thanks!

1 Like

You still can do mouse positions:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse() 
local Button = ... -- set this

Mouse.Button1Down:Connect(function()
    local x, y = Mouse.x, Mouse.y
    local Size, Pos = Button.AbsoluteSize, Button.AbsolutePosition
    
    if math.abs(Pos.X - X) <= Size.X and math.abs(Pos.Y - Y) <= Size.Y then
       -- TODO add the code for what happens when they DO click the button
    else
        -- TODO Add the code for what happens when they don't click the button
    end
 
end)



Let me be a little more precise with my intentions. I have a grid with buttons in a scrolling frame. Would this still work for all of them? Thanks!

A couple of changes of the code should make it work how you want

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse() 
local Buttons = ... -- set this
Mouse.Button1Down:Connect(function()
	local ButtonFound = false

	local X, Y = Mouse.x, Mouse.y

	for _, Button in ipairs(Buttons) do
		local Size, Pos = Button.AbsoluteSize, Button.AbsolutePosition
		if math.abs(Pos.X - X) <= Size.X and math.abs(Pos.Y - Y) <= Size.Y then
			ButtonFound = true
			-- TODO add the code for what happens when they DO click the button
		end
	end
	if not ButtonFound then
		-- TODO Add the code for what happens if they don't click a button
	end
end)

One final issue :sweat_smile:
I have a popup on top of my scrolling frame sometimes.
Do I add something like this?

if PopUp.Visible == true
   break -- or whatever the correct command is, either continue or return
end

Seems odd to have a problem with this…

--script inside the button
local button = script.Parent
button.MouseButton1Click:Connect(function()
	print("Clicked")
end)

No matter where I click it will not fire unless it’s on the button.

I need to fire a function when I click on the button, and another function when I click elsewhere.

Assuming the popup covers the entire frame, then yeah.

could there be any other solutions? this one seems… a bit too hardcoded? I know with raycasts, it returns the instance, or the part that the raycast hit. Is there something similar with the UI? Thanks!

If they click while the popup is active, should it trigger the code for button not pressed?

You can use StarterGui:GetGuiObjectsAtPosition()

I will look into this shortly, thanks!

elsewhere as in anyplace, even not on the gui?

Well my GUI takes up my entire screen, so elsewhere is any place but the button :D

sweet …

local hovering = false
local button = script.Parent

button.MouseEnter:Connect(function()
	hovering = true
end)

button.MouseLeave:Connect(function()
	hovering = false
end)

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		if hovering then
			print("Clicked Button")
		else
			print("Clicked somewhere else")
		end
	end
end)

I’m working on something similar:

UserInputService.InputBegan:Connect(function(input, gameProcessed)

	if input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then

		local guiObjectSelected = playerGui:GetGuiObjectsAtPosition(input.Position.X, input.Position.Y)[1]

		print(guiObjectSelected)
		
	end
	
end)

So, while I’m working on this, do yourself a favor, and don’t waste your time, as I might have a solution coming up :P

If this fails, I’ll try your approach :D

Thanks though!

Something I used myself, that’s why I asked. gl

Here’s my final code. Thank you all for the help!

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.Touch or input.UserInputType == Enum.UserInputType.MouseButton1 then

		local guiObjectSelected = PlayerGui:GetGuiObjectsAtPosition(input.Position.X, input.Position.Y)[1]

		if guiObjectSelected == AdditionalInfoSideBar.NameText then

			hidePopUp()

			return

		elseif guiObjectSelected == AdditionalInfoSideBar.LoadButton then

			hidePopUp()

			return

		elseif guiObjectSelected == Top.NewButton then

			PopUp.Visible = true

			return

		elseif guiObjectSelected == PopUp.BlankButton then

			isBasic = false
			ConfirmButton.Visible = true
			BlankButton.UIStroke.Enabled = true
			BasicButton.UIStroke.Enabled = false

			return

		elseif guiObjectSelected == PopUp.BasicButton then

			isBasic = true
			ConfirmButton.Visible = true
			BasicButton.UIStroke.Enabled = true
			BlankButton.UIStroke.Enabled = false

			return

		elseif guiObjectSelected == PopUp.ConfirmButton then

			if Debounce == false then

				Debounce = true 

				if isBasic == true then

					-- [redacted]

					return

				elseif isBasic == false then

					print("the blank vehicle doesn't exist yet")

					Debounce = false

					return

				end
			end

			return

		elseif guiObjectSelected == PopUp.CloseButton then

			hidePopUp()

			return

		elseif guiObjectSelected == Top or guiObjectSelected == ScrollingFrame or guiObjectSelected == AdditionalInfoSideBar or guiObjectSelected == MenuGUI.Frame or guiObjectSelected == MenuGUI.Background then 

			hidePopUp()

			AdditionalInfoSideBar.NameText.Text = ""
			AdditionalInfoSideBar.Created.Text = "Created: "
			AdditionalInfoSideBar.LastModified.Text = "Last Modified: "
			AdditionalInfoSideBar.TimeSpent.Text = "Time Spent: " 
			AdditionalInfoSideBar.LoadButton:SetAttribute("saveID","")

			return

		end

		for _, cell in ipairs(scrollingFrameCellsArray) do
			if guiObjectSelected == cell then

				AdditionalInfoSideBar.NameText.Text = cell:GetAttribute("Name")
				AdditionalInfoSideBar.Created.Text = "Created: " .. DateTime.fromUnixTimestamp(tonumber(cell:GetAttribute("Created"))):FormatLocalTime("MM-DD-YYYY\ HH:mm:ss","en-us")
				AdditionalInfoSideBar.LastModified.Text = "Last Modified: " .. DateTime.fromUnixTimestamp(tonumber(cell:GetAttribute("Modified"))):FormatLocalTime("MM-DD-YYYY\ HH:mm:ss","en-us")
				AdditionalInfoSideBar.TimeSpent.Text = "Time Spent: " .. secondsToDaysHoursMinutesSecondsString(cell:GetAttribute("Time_spent"))
				AdditionalInfoSideBar.LoadButton:SetAttribute("saveID",cell:GetAttribute("Created"))

				return

			end
		end

	end
end)
1 Like

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