Gun shot multiples everytime I un-equip and re-equip

When you fire, you atleast fire one round at a time, and when you re-equip it, it multiples to two shots.

wait(game.Loaded)

local Settings = require(script.Parent)

--[[ Services ]]

local Players = game:GetService("Players")
local UserInputServices = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

--[[ Variables ]]

local Tool = script.Parent.Parent

local Parent    =  script.Parent 

local Player    =  Players.LocalPlayer
local Character =  Player.Character
local Humanoid  =  Character.Humanoid

local Mouse     =  Player:GetMouse()

local Magazine  =  30
local Magazine_Used = 0
local Ammo      =  9999

Magazine        =  Settings.Max_Magazine
Ammo            =  Settings.Max_Ammo

local Events    =  Parent.Events
local Fire      =  Events.Fire

local Sounds    =  Parent.Sound
local Empty_Gun =  Sounds.Empty_Gun
local Gun_Reload=  Sounds.Gun_Reload

local Animation = Parent.Animation
local Animator = Humanoid:WaitForChild("Animator")
local AKMHOLDING = Animation["AKM - Holding"]; local AnimTrack1 = Animator:LoadAnimation(AKMHOLDING)
local AKMFIRING = Animation["AKM - Firing"]; local AnimTrack2 = Animator:LoadAnimation(AKMFIRING)
local AKMRELOAD = Animation["AKM - Reload"]; local AnimTrack3 : AnimationTrack = Animator:LoadAnimation(AKMRELOAD)

local Weapon_Hud=  Tool["Weapon HUD"]

--[[ Recoil ]]

local camera = workspace.CurrentCamera
local random = Random.new()

local spread_amount = math.rad(5)

local recoil_amount = 0.05

local recoil_acc = CFrame.Angles(0, 0, 0)

local recoil_decay = 0.85

--[[ Key ]]

local KeyCode = Enum.KeyCode
local UserInputType = Enum.UserInputType

local Fire_Key   =  UserInputType.MouseButton1
local Reload_Key =  KeyCode.R
local Aim_Key    =  UserInputType.MouseButton2

local Tool_Equipped = Character:FindFirstChild(Tool.Name)

--[[ Boolean ]]

local Reload    =  false
local Held      =  false
local Shoot     =  false

--[[ Functions ]]

function ShootGun()

	local shoot_cf = camera.CFrame * CFrame.Angles( 
		random:NextNumber(-recoil_amount, recoil_amount),
		random:NextNumber(-recoil_amount, recoil_amount),
		0
	)
	local shoot_ray = Ray.new(shoot_cf.p, shoot_cf.LookVector)

	camera.CFrame = camera.CFrame * recoil_acc:inverse()

	recoil_acc = recoil_acc * CFrame.Angles(recoil_amount, 0, 0)

	camera.CFrame = camera.CFrame * recoil_acc

end


--[[ Main Code ]]

Tool.Equipped:Connect(function()
	
	AnimTrack1:Play()
	
	local Cloned_GUI = Weapon_Hud:Clone()
	Cloned_GUI.Parent = Player.PlayerGui
	
	UserInputServices.InputBegan:Connect(function(Input, GameProccessed)

		if GameProccessed then return end

		if Input.UserInputType == Fire_Key then

			if Reload == false and Magazine >= 1 then
				
				Shoot = true
				
				if Magazine <= 0 then
					
					Shoot = false
					
				end
				
			end
		end
		
		if Input.KeyCode == Reload_Key then
			
			if Reload == false and Magazine < Settings.Max_Magazine then
				
				AnimTrack3:Play(1,3,.3)

				Gun_Reload:Play()

				Reload = true
				Shoot = false

				wait(Settings.Reload_Cooldown)

				Ammo -= Magazine_Used
				Magazine_Used = 0
				Magazine = Settings.Max_Magazine
				
				Reload = false

			end
			
		end

	end)
	
	UserInputServices.InputEnded:Connect(function(Input, GameProccessed)

		if GameProccessed then return end
		
		if Input.UserInputType == Fire_Key then

			if Reload == false then

				Shoot = false

			end
		end
		
	end)
	
	while true do
		
		if Shoot and Magazine >= 1 and Character:FindFirstChild(Tool.Name)  then
			
			AnimTrack2:Play(1,1,1.25)
			
			Fire:FireServer(Mouse.Hit.Position, Tool:FindFirstChild("Handle"))
			
			Magazine -= 1 Magazine_Used += 1
			
			ShootGun()
			
			wait(Settings.Fire_Per_Second)

		end
		
		pcall(function()
		
		Cloned_GUI.Background.Magazine.Text = Magazine
		Cloned_GUI.Background.Ammo.Text = Ammo
		Cloned_GUI.Background.Gun_Name.Text = Tool.Name
		
		wait()
			
		end)
		
		
		wait()
	end
	
end)

Tool.Unequipped:Connect(function()
	
	AnimTrack1:Stop()
	
	Shoot = false
	
	for i,v in pairs(Tool:FindFirstChild("Particle Part"):GetChildren()) do

		v:Destroy()

	end
	
	local Find_GUI = Player.PlayerGui:FindFirstChild("Weapon HUD")
	
	if Find_GUI then
		
		Find_GUI:Destroy()
		
	end
end)

RunService.RenderStepped:Connect(function(dt)

	camera.CFrame = camera.CFrame * recoil_acc:inverse() 

	recoil_acc = recoil_acc:Lerp(CFrame.new(), recoil_decay * dt)

	camera.CFrame = camera.CFrame * recoil_acc
	
end)

Get this out of the .Equipped event otherwise it will fire every time you equip your tool. Instead, add a condition checking if the tool is equipped inside the InputBegan event.

P.S.: Same thing with your while loop.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.