Gun Script First Shot Glitch

Hi!, I am creating a gun, and for some reason, when I click for the first shot, it fires infinitely. However, when I click again, it works as it is meant to from then on. There are no errors.

Here is the code:

--[[Variables]]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local tool = script.Parent -- get tool
local player = game:GetService("Players").LocalPlayer -- get player
local mouse = player:GetMouse() -- getting the mouse
local sound = tool:WaitForChild("GunFire") -- gunshot sound
local torso = "" -- nothing
local reloading = false -- checks if reloading or not
local contextActionService = game:GetService("ContextActionService")
local bodyType = nil -- nil for now but will check whether player is R6 or R15
local difference = 0 -- space between head and mouse
local bullets = tool:WaitForChild("Bullets")
local replicatedStorage = game:GetService("ReplicatedStorage")
local events = replicatedStorage:WaitForChild("Events")
local functions = replicatedStorage:WaitForChild("Functions")
local akEvents = events:WaitForChild("AKEvents")
local akFunctions = functions:WaitForChild("AKFunctions")
local gunGui = tool:WaitForChild("AKGUI")
local reloadTime = "3"
local down = false
-- Remote Events --
local equipAnimation = akEvents:WaitForChild("EquipAnimation")
local headshot = akEvents:WaitForChild("Headshot")
local reloadTwo = akEvents:WaitForChild("Reload")
local shootEvent = akEvents:WaitForChild("ShootEvent")
local unequipAnimation = akEvents:WaitForChild("UnequipAnimation")
-- Remote Functions --
local checkBodyType = akFunctions:WaitForChild("CheckBodyType")
local fetchBulletsLeft = akFunctions:WaitForChild("FetchBulletsLeft")
--[[Find Body Type]]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function findBodyType()
	bodyType = checkBodyType:InvokeServer(tool)
	print(bodyType)
end
--[[Reload Function]]------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function reload()
	if bullets.Value < 31 then
	reloading = true
	reloadTwo:FireServer(tool.Reload)

	player.PlayerGui:WaitForChild("AKGUI").Bullets.Text = "RELOADING"
	wait(reloadTime)
	bullets.Value = 31
	player.PlayerGui:WaitForChild("AKGUI").Bullets.Text = "AK-47 - " .. bullets.Value.. "/31"
	mouse.Icon = game.ReplicatedStorage.MouseValueDeagle.Value
	equipAnimation:FireServer(tool.Shoot)
	reloading = false
	end
end
--[[Main Firing Script]]---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

tool.Equipped:Connect(function(theMouse)

	gunGui:Clone().Parent = player.PlayerGui
	findBodyType()
	equipAnimation:FireServer(tool.Shoot)
	local img = game.StarterGui.ImageLoader.DeagleHair.Image
	mouse.Icon = img
	mouse.Button1Down:Connect(function()
down = true
	while down == true do
		wait(.1)

			print("working")
		if bullets.Value <= 0 or reloading == true then
			--Don't do anything
		else
			
			local head = game.Workspace[player.Name].Head.CFrame.lookVector
			local theMouse = CFrame.new(game.Workspace[player.Name].Head.Position,theMouse.Hit.p).lookVector
			difference = (head - theMouse)
			local ray = Ray.new(tool.Handle.CFrame.p, (player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*250)
			local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false, true)
			
			--if difference.magnitude < 1.33 then
				sound:Play()
				bullets.Value = bullets.Value - 1
				shootEvent:FireServer(tool, position, part)
			
mouse.Button1Up:connect(function()
	down = false
end)
				end
		end
		--end
	end)

	

	
	local reloadMobileButton = contextActionService:BindAction("ReloadBtn",reload,true,"r")
	contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
	contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419")
end)
--[[Tool Unequipped Event]]--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
tool.Unequipped:Connect(function()
	mouse.Icon = ""
	unequipAnimation:FireServer(tool.Shoot)
	player.PlayerGui.AKGUI:Destroy()
	contextActionService:UnbindAction("ReloadBtn")
end)

“Fires infinitely”?
“it works as it is meant to”?

The first click, if i click the nrelease, not hold it down, it fires 12 or so shots. Its meant to be automatic, LIke as I hold mousebutton it fires.

You have this snippet

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

But you are listening to mouse.Button1Up after wait(0.1), which means the first time you click, if you let go within 0.1 seconds, down will never be set to false.

Move your code connecting to mouse.Button1Up so it’s outside of the mouse.Button1Down function, instead of being inside it.

1 Like

Plus you’ll have the added benefit of not creating a new connection which doesn’t get cleaned up every time you click. :slight_smile:

It worked! Thank you so much, man!