Mousebutton1down not being detected on the first mouseclick/mouse hold?

I made an automatic raycast gun but for some reason when you first click/hold click, it doesn’t fire. The clicks/mouse holds after the first click/mouse hold are perfectly functional.

CODE:

t.Parent.Equipped:Connect(function(m)
	
m.Button1Down:connect(function()

local mouseDown = false
local canPlay = true

m.Button1Down:Connect(function()
    mouseDown = true
end)

m.Button1Up:Connect(function()
    mouseDown = false
end)

while true do
			wait()	
		if mouseDown == true then print("d") end
		if mouseDown then
        if canPlay then
        canPlay = false
        if equip_value == true or reloading or toggle == false or chamber.IsPlaying  then return end
		if ammo <= 0 or chambered == false then t.Parent.Events.Empty:FireServer() return end
		if not running then
		running = true;
		self = t.Parent.Parent;
		local ray = Ray.new((muzzle.CFrame.p + Vector3.new(0, 0, 0)), ((m.Hit.p - muzzle.CFrame.p).unit * 100))
		local hit, position = workspace:FindPartOnRay(ray, self);
		local spread_amount = math.random(-1.5, 1.5)
		position = Vector3.new(position.X + (spread_amount * spread_amount * math.random()),position.Y + (spread_amount * spread_amount * math.random()),position.Z + (spread_amount * spread_amount * math.random()))	
		pcall(function()
		if hit.Parent.Humanoid then
			t.Parent.Events.Damage:FireServer(hit, damage);
			end
			end);
			shoot:Play()
			t.Parent.Events.Laser:InvokeServer(self, position, muzzle);
			t.Parent.Events.Shoot:FireServer()
			ammo-=1
			if ammo == 0 and reserve == 0 then
				game.Players.LocalPlayer.PlayerGui.ammoGui.Chamber.Text = "NOT CHAMBERED"
			end
			game.Players.LocalPlayer.PlayerGui.ammoGui.Ammo.Text = ammo
			t.Parent.Events.Bullet:FireServer()	
			ShootRecoil()
			print(ammo)
			wait(cool - .1);
			running = false;
            canPlay = true
        end
    end
	end
		end
		end)
1 Like

Its because you did the Button1Down event twice and one of them sets mouseDown variable with the value of false, therefore it will take the other button1down to make it true

2 Likes