Holding down left click, tool repeats function until mouse is let go of?

I know, the title sounds wierd, but I’ll explain it to you. Let’s say I have a gun tool, and I want it to be sort of like an AR where, you hold down the trigger (left click) and the gun fires repeatedly. How would I do that?

2 Likes

Here’s some general hold-to-shoot-gun pseudocode that might help:

local firing = false
local fireRate = 20 -- shots/second

local function ShootBullet(where)
	-- whatever code you use to fire a single bullet in a certain direction
end

LeftMouseDownEvent:Connect(function()
	if (not firing) then
		firing = true -- the 'firing' flag makes sure this loop doesn't run multiple times concurrently.
		while (firing) do
			ShootBullet(MousePointDirection)
			wait(1 / fireRate)
		end
	end
end)

LeftMouseUpEvent:Connect(function()
	firing = false -- stop shooting
end)
3 Likes