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

How to run a function as long as a player hold down mouse button1?

I know I asked this before with keys, but when I do mouse.button1down, it keeps saying it being pressed when I'm not pressing it .-.

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

function mousedown()
	while true do
		wait()
		local hold = false
		if mouse.Button1Down then
		print('on')
		hold = true
		end
		if mouse.Button1Up then
		hold = false
		end
		if hold == true then
		onActivated()
		end
	end
end

1 answer

Your problem is on line 8 and 12. Simply putting mouse.Button1Down or mouse.Button1Up does not return a boolean value of true or false, rather just states that it is in fact an event of mouse (try printing mouse.Button1Down or mouse.Button1Up).

In order to check if it is being held, we will need a variable that a Button1Down event changes to true and a Button1Up changes to false:

local mouse = game.Players.LocalPlayer:GetMouse()
local button1down = false

mouse.Button1Down:connect(function()
	button1down = true
	local timedown = 0
	while button1down do
		--code
	end
end)

mouse.Button1Up:connect(function()
	button1down = false
end)

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