Automatic script for gun doesnt work

Hello so i’ve got this script (local script) in my gun:

local gun = script.Parent
local empty_sound = game.ReplicatedStorage.clip_empty
local reload_sound = game.ReplicatedStorage.Reload
local player = game.Players.LocalPlayer
local clipSize = gun.Ammo.Value
local ammo = gun.Ammo
local shooting = false
local equipped = false
--UserInputService Setup
local userInput = game:GetService('UserInputService')
--Mouse Icon
local Camera = game:GetService("Workspace").CurrentCamera
local mouse = game.Players.LocalPlayer:GetMouse()
local normalSpeed = 8
local AimingSpeed = 4
--Remote Event Setup
local Character = player.Character or player.CharacterAdded:Wait()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild('SwitchEvent')
local AimingScript = Character:WaitForChild("AimingScript")
--Checks if Mouse is clicked
gun.Equipped:Connect(function(mouse)
	script.Enabled = true
	player.PlayerGui.ScreenGui.Ammo.Visible = true
	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
	equipped = true
	shooting = true
	mouse.Button1Down:Connect(function()
		if gun.Ammo.Value > 0 then
			while gun.Ammo.Value > 0 and shooting do 
				shooting = true
				task.wait(0.1)
				remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
				local clone = game:GetService("ReplicatedStorage").BulletModels.TheMainBulletXD:Clone()
				clone.CFrame = gun.Flash.CFrame
				clone.Parent = game.Workspace.BulletFolders
				clone.Orientation = gun.Handle.Orientation
				Camera.CFrame = Camera.CFrame * CFrame.Angles(0.01,math.rad(1),0)
				gun.Flash.PointLight.Enabled = true
				gun.Ammo.Value -= 1
				gun.otherFlash.Transparency = 0
				gun.otherFlash.PointLight.Enabled = true
				wait(0.01)
				gun.otherFlash.Transparency = 1
				gun.otherFlash.PointLight.Enabled = false
				game.Lighting.GunCorrection.Enabled = false
			end
		else
			empty_sound:Play()
		end
		end)
	mouse.Button1Up:Connect(function()
		gun.otherFlash.Transparency = 1
		gun.otherFlash.PointLight.Enabled = false
		game.Lighting.GunCorrection.Enabled = false
		player.PlayerGui.WhiteFlashForGun.Enabled = false
		gun.otherFlash.Transparency = 1
		shooting = false
	end)
	
	mouse.Button1Down:Connect(function()
	end)
	
	mouse.Button2Down:Connect(function()
		if player.Character:FindFirstChildWhichIsA("Humanoid") then
			player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = AimingSpeed


		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 40



	end
		end)



	mouse.Button2Up:Connect(function()
		if player.Character:FindFirstChildWhichIsA("Humanoid") then
			player.Character:FindFirstChildWhichIsA("Humanoid").WalkSpeed = normalSpeed


		local camera = game.Workspace.CurrentCamera



		camera.FieldOfView = 70



	end



end)



-- Unequip gun



gun.Unequipped:Connect(function()
		gun.otherFlash.Transparency = 1
		gun.otherFlash.PointLight.Enabled = false
		game.Lighting.GunCorrection.Enabled = false
	player.PlayerGui.WhiteFlashForGun.Enabled = false
	gun.Flash.PointLight.Enabled = false
	local camera = game.Workspace.CurrentCamera
	camera.FieldOfView = 70
	player.PlayerGui.ScreenGui.Ammo.Visible = false
	reload_sound:Stop()
	empty_sound:Stop()
end)





--Checks if the letter R is pressed to reload






-- Update ammo GUI



ammo.Changed:Connect(function()

	player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)
	end)
end)


mouse.Button1Down:Connect(function()
	gun.otherFlash.Transparency = 0
	gun.Flash.PointLight.Enabled = false
	player.PlayerGui.WhiteFlashForGun.Enabled = false
	gun.otherFlash.Transparency = 1
	script.Enabled = true
end)

and i’m experiencing an error in which whenever i stop shooting i gotta requip the gun for it to shoot again.

1 Like