Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you detect if the left mouse button is being held down?

Asked by 4 years ago

I am having trouble with this and I know it's a simple script but I can't find any answers out there. I would like to learn from this. Thank you.

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
mouse = game:GetService("Players").LocalPlayer:GetMouse()

mouse.Button1Down:Connect(function()
-- skript here
end)

for up use

mouse = game:GetService("Players").LocalPlayer:GetMouse()

mouse.Button1Up:Connect(function()
-- skript here
end)

Remember it needs to be local script I hope this helps.

0
I need help with a hold script, the script you just gave me where just individual up/down clicks. iinaoriku 52 — 4y
0
make a question for the problem :) Igoralexeymarengobr 365 — 4y
Ad
Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago

Utilize the Button*Down/Up methods on the Mouse, obtained by calling :GetMouse() on the LocalPlayer object.

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

With this function, we can check if the Button1Down is fired.

local Mouse = game:GetService('Players').LocalPlayer:GetMouse();
local BeingHeldDown = false;
Mouse.Button1Down:Connect(function(DownFunction)
    BeingHeldDown = true;
end)

Mouse.Button1Up:Connect(function(UpFunction)
    BeingHeldDown = false;
end);

If we use a variable to remember the state of the mouse, this will allow us to check if the player is holding down the left mouse button. This will allow us to do cool loops like this:

while BeingHeldDown do
    wait()
    print('Mouse is being held down!')
end

or something like this:

game:GetService('UserInputService').InputBegan:Connect(function(Key, Process)
    Key = Key.KeyCode -- I don't need anything else that Key provides

    if (Key == Enum.KeyCode.E) and BeingHeldDown then
        print('The E button is being pressed while the MouseButton1 is being held.')
    end
end)

Using variables to log event states can prove to be very useful in many cases.

Answer this question