I'd like to know if there's an event for TextButtons/ImageButtons that fire when the left mouse button is held down, as in you hold it down for a couple of seconds. I know MouseButton1Down
, but that's not suiting for what I'm trying to do. What I'm trying to do is while the left mouse button is held down, a dummy model is rotated. The current script only fires when the mouse is clicked and not held down.
1 | local dummy = workspace.Dummy |
2 | script.Parent.MouseButton 1 Click:Connect( function () |
3 | dummy:SetPrimaryPartCFrame(dummy.PrimaryPart.CFrame * CFrame.fromEulerAnglesXYZ( 0 ,math.rad( 3 ), 0 )) |
4 | end ) |
Not sure if there's any easier way to do it, but all I've found was using UserInputService
01 | local inputService = game:GetService( "UserInptuService" ) |
02 |
03 | local mouseDown = false |
04 |
05 | inputService.InputBegan:Connect( function (input)) |
06 | if (input.UserInputType = = Enum.UserInputType.MouseButton 1 ) then |
07 | mouseDown = true |
08 | while mouseDown = = true do |
09 | -- your code |
10 | end |
11 | end |
12 | end ) |
13 |
14 | inputService.InputEnded:Connect( function (input) |
15 | if (iinput.UserInputType = = Enum.UserInputType.MouseButton 1 ) then |
16 | mouseDown = false |
17 | end |
18 | end |
Explained:
1 | if (input.UserInputType = = Enum.UserInputType.MouseButton 1 ) then |
Checks if the key pressed is the mouse's right button
1 | mouseDown = true |
2 | while mouseDown = = true do |
3 | -- your code |
4 | end |
changes the "mouseDown" variable's value to true, and lets your things happen while it's down/being held
1 | inputService.InputEnded:Connect( function (input) |
2 | if (iinput.UserInputType = = Enum.UserInputType.MouseButton 1 ) then |
3 | mouseDown = false |
4 | end |
5 | end |
Detects when the mouse button is no longer being held, and when it does, changes the variable's value, stopping the loop
Let me know if you need any extra help!
While UserInputService is good to know, this might be an easier way to understand.
01 | m 1 down = false |
02 |
03 | function doThing() |
04 | while m 1 down do |
05 | --Thing |
06 | wait() |
07 | end |
08 | end |
09 |
10 | script.Parent.MouseButton 1 Down:Connect( function () m 1 down = true doThing() end ) |
11 | script.Parent.MouseButton 1 Up:Connect( function () m 1 down = false end ) |