Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i detect if a player is HOLDING left click on a gui button, then letting go?

So I want to create a button, it plays a looped sound until the player lets go of their mouse. I have tried using InputEnded on the button, to no avail.

I may be able to do this via UserInputService, however, I'd also like to ask if detecting the player's mouse coordinates are possible on touch devices?

thanks

1 comment

2 answers

✓ Accepted answer

Using UserInputService's events here would be completely unnecessary. The InputBegan and InputEnded events of GUIButtons should be all you need. For only mouse input, you could as well just use the MouseButton1Down and MouseButton1Up events (Roblox might fire these mouse events even for touch input but they really shouldn't be used for such).

local function isValidInput(inputObject)
	return	inputObject.UserInputType == Enum.UserInputType.MouseButton1
		or	inputObject.UserInputType == Enum.UserInputType.Touch
end

button.InputBegan:Connect(function(inputObject)
	if isValidInput(inputObject) then
		sound:Play()
		
		button.InputEnded:Wait()
		
--[[
		--	Alternatively the following can be used to let the sound play even when the mouse is moved off the button
	
		local endedConnection
		endedConnection = button.InputEnded:Connect(function(inputObject)
			if isValidInput(inputObject) then
				endedConnection:Disconnect()
			end
		end)
		
		repeat wait() until not endedConnection.connected
--]]
		
		sound:Stop()
	end
end)

Yes if you set your “held=“ variable and set it to true, then loop it until it =false. Or possibly...

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local held = false
mouse.Button1Down:connect(function()
     held = true
end)

mouse.Button1Up:connect(function()
     held = false
end)

spawn(function()
     while true do
          wait(.1)
          print(held)
     end
end)

4 comments

0

Not what i asked, sorry.

0

I am trying to do with a button, and the method you're described is inefficient. You can use UserInputService's InputBegan and InputEnded events, however I need it done with a button.

0

Ok, i misread, I was thinking part not gui button

0

Mouse x and y and mouse.hit should work the same on mobile though

This discussion is preserved as it was. New answers, comments, and votes are closed.