Mouse hold for my gun system will not function (SOLVED)

hello all, me and my friend made a gun system, and every gun is just a single shot but i want to to auto Aswell, i tried doing the

local holding = false

mouse.Button1Down:Connect(function()
holding = true
end)
mouse.Button1Up:Connect(function()
holding = false
end)

and it still functions like a regular single shot gun.
here is the source code if you need it

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local mouseHold = false
local shooting = false
local serverRemote = script.Parent:WaitForChild("Events").Shoot
local shootAnim = script.Parent.Animations.Shoot
local Animation = script.Parent.Animations.Idle
local equipAnimation = script.Parent.Animations.Equip
local uis = game:GetService("UserInputService")
local CD = false

local function testFire()
	local direction = mouse.Hit.Position
	local LoadAnimation = player.Character:WaitForChild("Humanoid"):LoadAnimation(shootAnim)
	local reloadANimation = script.Parent.Animations.Reload
	loadReloadForFire = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(reloadANimation)

	serverRemote:FireServer(direction)
	LoadAnimation:Play()

	if loadReloadForFire.IsPlaying  then
		loadReloadForFire:Stop()
	end

	script.Parent.Sounds["Shoot"]:Play()

	print("bullet has been shot")
	print(direction)
end

mouse.Button1Down:Connect(function(MouseSource)
	mouseHold = true
	if script.Parent.IsEquipped.Value == true and CD == false and mouseHold == true then
		if script.Parent.Stats.Ammo.Value > 0  then
			--CD = true
			testFire()
			--wait(0.1)
			--CD = false
		end

		uis.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.R then
				if script.Parent.Stats.Ammo.Value < script.Parent.Stats.Mag.Value then
					script.Parent.Events.Reload:FireServer()
					local reloadANimation = script.Parent.Animations.Reload
					loadReload = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(reloadANimation)
					loadReload:Play()
					script.Parent:WaitForChild("Sounds").Reload:Play()
				end
			end
		end)
	end
end)

mouse.Button1Up:Connect(function()
	mouseHold = false
end)

local function OnEquip()
	animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(Animation) 
	equipanimation  = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(equipAnimation)

	equipanimation:Play()

	mouse.Icon = "rbxassetid://409468479"

	if equipanimation.Stopped then
		animation:Play()
	end
	script.Parent.IsEquipped.Value = true
	script.Parent.Sounds["Equip"]:Play()
end

local function OnUnequip()
	animation:Stop()
	equipanimation:Stop()
	loadReload:Stop()
	mouse.Icon = "rbxassetid://"
	script.Parent:WaitForChild("Sounds").Reload:Stop()
	local activeTracks = player.Character.Humanoid:GetPlayingAnimationTracks()
	for _,v in pairs(activeTracks) do
		v:Stop()
	end
end

script.Parent.Equipped:Connect(function()
	OnEquip()
end)

script.Parent.Unequipped:Connect(function()
	OnUnequip()
end)

If you’re wondering no, no errors according to the output or console.

Use a loop to make it rapid fire. You might have to disconnect some connections if there are memory issues. Handling Events.

1 Like

Using a boolean value is relatively questionable and can make your code’s maintainability a mess later on. Further, the Mouse object that you get from the player has never really been too reliable for reporting information (again, though, mostly due to maintainability).

If possible, consider using the IsMouseButtonPressed method in a function tied to BindToRenderStep.

Also, the following as side notes for better and easier-to-read code:

  • Disconnect your connections when you’re done.
  • You don’t need to write “if (bool == true)” - “if (bool)” will work exactly the same . . since that’s what they’re made for.
  • Check for ammunition counts not only on the client, but also the server.

yes, the ammo count is also on the server side

1 Like

thank u so much it worked with disconnect

1 Like

Thank you Aswell for also explaining

1 Like

thank u so much it worked with disconnect

I’m glad you got it working!

Thank you Aswell for also explaining

Of course!