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 tell when someone is holding their mouse down?

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

✓ Accepted answer

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);

2 comments

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.

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.

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