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

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

2 answers

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.

2 comments

0

I need help with a hold script, the script you just gave me where just individual up/down clicks.

0

make a question for the problem :)

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.

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