Can i detect if a player I holding down a GUI Button

Just wondering if theres an function to check if a player is holding down a button. I did a little bit of research but i can only find if players are holding down keys.

Anyone know?

6 Likes

Use the MouseButton1Down and MouseButtonUp Event.
This is a simple example:
Gui example:

local Button = script.Parent.TextButton
local MouseBeingHeldDown = false

Button.MouseButton1Down:Connect(function()
	MouseBeingHeldDown = true	
	while MouseBeingHeldDown == true do
		print("Mouse is being held down.")
		wait()
	end
end)

Button.MouseButton1Up:Connect(function()
	MouseBeingHeldDown = false
end)


Mouse example(Can work with any part in the workspace.):

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local MouseButton1BeingHeldDown = false

Mouse.Button1Down:Connect(function()
	if Mouse.Target.Name == "Part" then-- Change Part to the button part.
		MouseButton1BeingHeldDown = true
		while MouseButton1BeingHeldDown == true do
			print("Mouse is being held down.")
			wait()
		end
	end
end)

Mouse.Button1Up:Connect(function()
	MouseButton1BeingHeldDown = false
end)
4 Likes

You can use userinputservice or mousebutton1down. I prefer userinputservice but it’s your choice.

Person above @SpacialEthanRB gave a nice example of mousebutton1down.

UserInputService would go something like this

local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input)
if input.UserInputType = Enum.UserInputType.MouseButton1Down then
print('Mouse button1 was pressed')
end

You can detect if the InputChaged ot InputEnded. Either example is fine. You’ll get the same result.

Im making my game compatible for phone so i was just wondering does this function work for phone aswell?

Yes UserInputService does that. It can adapt to the type of input a person has. Mobile or Laptop.

https://developer.roblox.com/en-us/api-reference/enum/UserInputType
https://developer.roblox.com/en-us/api-reference/event/UserInputService/TouchTap

Articles to help.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input, gameprocessed)
if input.UserInputType = Enum.UserInputType.MouseButton1 then
print("MouseButton1!")
elseif input.UserInputType = Enum.UserInputType.Touch Then
print("Touch!")
end
6 Likes

Thank you this works perfectly

1 Like

How to detect if the player stopped pressing a button?

1 Like

Oh, never mind. I just had to use InputEnded. Sorry.