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

How can I tell when someone is holding their mouse down?

Asked by 7 years ago

This is for a gun I'm making, but I want it to be an automatic rifle, as in it will keep shooting if you hold it down.

But I don't know how to tell if someone is.

Is there like an event or something to tell?

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

I use something like this: Basically you need to spawn a loop that checks if a variable called "MouseDown" is true, and when the mouse event Button1Down is called, set the variable to true, but when Button1Up is called, set the variable to false.

As long as this is in a local script it will work.

EDIT: Added a renderstepped wait to the loop

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

local MouseDown = false;

local rs = game["Run Service"].RenderStepped; -- Faster loop
function MouseChecker()
    while true do
        rs:wait();
        if (MouseDown == true) then
            print("Mouse is down");
            -- repeat code here while the mouse is down
        end
    end
end

Mouse.Button1Down:Connect(function()
    MouseDown = true;
end)

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

spawn(MouseChecker);
1
For future reference, it'd be better to integrate your "MouseChecker" in a run service step update, rather than an ongoing loop. I'd suggest using BindToRenderStep, with the Input render priority value. ScriptGuider 5640 — 7y
0
Yeah, I know there are more efficient & responsive ways to do it but I wasn't really focusing on maximum efficiency, it was more of an example solution. plasma_node 343 — 7y
Ad

Answer this question