CasingMesh.Parent=CasingBase function OnFire()if IsShooting then return end if MyHumanoid and MyHumanoid.Health>0 then if RecoilTrack and AmmoInClip>0 then end IsShooting=true if AmmoInClip>0 and not Reloading then if Handle:FindFirstChild("\070\105\114\101\083\111\117\110\100")then Handle.FireSound:Play()end CreateFlash()Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent),"\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\092\071\117\110\087\097\105\116\067\117\114\115\111\114\046\112\110\103")local E=MOUSE_TARGET for A=1,BulletCount,1 do local m=(E-Handle.Position).unit m=((CFrame.new(Vector3.new(0,0,0),m)*CFrame.Angles(0,0,(math.random()*2)*math.pi))*CFrame.Angles(math.random()*Spread,0,0)).lookVector local Y,h=RayCast(Handle.Position,m,Range)local J if Y then J=CreateBullet(h,Y)end if Y and Y.Parent then local E=Y.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")or Y.Parent.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if E then local A=game.Players:GetPlayerFromCharacter(E.Parent)if not GLib.IsTeammate(MyPlayer,A)then GLib.TagHumanoid(MyPlayer,E,1)E:TakeDamage(Damage)if J then J:Destroy()J=nil end end end end end local A=CasingBase:Clone()A.Position=Tool.Handle.Position+Vector3.new(0,1,0)A.Velocity=(Vector3.new(math.random()-.5,.5+math.random(),math.random()-.5)-1.5*(Tool.Handle.CFrame*CFrame.Angles(0,math.pi/4,0)).lookVector)*20 DebrisService:AddItem(A,2.5)A.Parent=game.Workspace AmmoInClip=AmmoInClip-1 UpdateAmmo(AmmoInClip)wait(FireRate-.2)if AmmoInClip>0 then if PumpTrack then PumpTrack:Play()end if Handle:FindFirstChild("\080\117\109\112\083\111\117\110\100")then Handle.PumpSound:Play()end wait(.2)end Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent),"\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\092\071\117\110\067\117\114\115\111\114\046\112\110\103")end IsShooting=false if AmmoInClip==0 then Reload()end if RecoilTrack then RecoilTrack:Stop()end end end function UpdateAmmo(E)if WeaponGui and(WeaponGui:FindFirstChild("\065\109\109\111\072\117\100")and WeaponGui.AmmoHud:FindFirstChild("\067\108\105\112\065\109\109\111"))then WeaponGui.AmmoHud.ClipAmmo.Text=AmmoInClip end end function OnMouseDown()LeftButtonDown=true OnFire()end function OnMouseUp()LeftButtonDown=false end function OnKeyDown(E)if string.lower(E)=="\114"then Reload()end end Tool.Input.OnServerEvent:connect(function(E,A,...)if E.Character==Tool.Parent then if A=="\077\111\117\115\101\049"then local E,A=...if E then OnMouseDown()else OnMouseUp()end elseif A=="\075\101\121"then local E,A=...if E then OnKeyDown(A)end elseif A=="\077\111\117\115\101\077\111\118\101"then local E=...MOUSE_TARGET=E end end end)function OnEquipped()EquipAnim=WaitForChild(Tool,"\069\113\117\105\112")PumpAnim=WaitForChild(Tool,"\082\101\108\111\097\100")FireSound=WaitForChild(Handle,"\070\105\114\101\083\111\117\110\100")MyCharacter=Tool.Parent MyPlayer=(game:GetService("\080\108\097\121\101\114\115")):GetPlayerFromCharacter(MyCharacter)MyHumanoid=MyCharacter:FindFirstChild("\072\117\109\097\110\111\105\100")MyTorso=MyCharacter:FindFirstChild("\084\111\114\115\111")WeaponGui=(WaitForChild(Tool,"\087\101\097\112\111\110\072\117\100")):Clone()if WeaponGui and MyPlayer then WeaponGui.Parent=MyPlayer.PlayerGui UpdateAmmo(AmmoInClip)end if EquipAnim then Tool.Grip=CFrame.new(.132808685,.00749024749,.526850462,.925221086,.000443253666,.379427731,.00859767944,.999718785,-0.0221330915,-0.379331142,.0237402022,.924956799)EquipTrack=MyHumanoid:LoadAnimation(EquipAnim)EquipTrack:Play()end if PumpAnim then PumpTrack=MyHumanoid:LoadAnimation(PumpAnim)end Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent),"\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\092\071\117\110\067\117\114\115\111\114\046\112\110\103")end

CasingMesh.Parent = CasingBase

function OnFire()
	if IsShooting then return end
	if MyHumanoid and MyHumanoid.Health > 0 then
		if RecoilTrack and AmmoInClip > 0 then
			
		end
		IsShooting = true
		if AmmoInClip > 0 and not Reloading then
			--if RecoilTrack then RecoilTrack:Play() end
			if Handle:FindFirstChild('FireSound') then
				Handle.FireSound:Play()
			end
			CreateFlash()
			Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunWaitCursor.png")
			local targetPoint = MOUSE_TARGET
			for i = 1, BulletCount do
				local shootDirection = (targetPoint - Handle.Position).unit
				-- Adjust the shoot direction randomly off by a little bit to account for recoil
				-- moves random*spread in a random angle from center shot point (center bias)
				shootDirection = ((CFrame.new(Vector3.new(0,0,0),shootDirection) * CFrame.Angles(0,0,math.random()*2*math.pi))*CFrame.Angles(math.random()*Spread,0,0)).lookVector
				local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range)
				local bullet
				-- Create a bullet here
				if hitObject then
					bullet = CreateBullet(bulletPos, hitObject)
				end
				if hitObject and hitObject.Parent then
					local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid") or hitObject.Parent.Parent:FindFirstChild'Humanoid'
					if hitHumanoid then
						local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
						if not GLib.IsTeammate(MyPlayer, hitPlayer) then
							GLib.TagHumanoid(MyPlayer, hitHumanoid, 1)
							hitHumanoid:TakeDamage(Damage)
							if bullet then
								bullet:Destroy()
								bullet = nil
								--bullet.Transparency = 1
							end
						end
					end
				end
			end

			local casing = CasingBase:Clone()
			casing.Position = Tool.Handle.Position + Vector3.new(0,1,0)
			casing.Velocity = (Vector3.new((math.random()-.5),(.5+math.random()),(math.random()-.5)) - 1.5*(Tool.Handle.CFrame * CFrame.Angles(0,math.pi/4,0)).lookVector)*20
			DebrisService:AddItem(casing, 2.5)
			casing.Parent = game.Workspace

			AmmoInClip = AmmoInClip - 1
			UpdateAmmo(AmmoInClip)
			wait(FireRate-.2)

			if AmmoInClip > 0 then
				if PumpTrack then
					PumpTrack:Play()
				end
				if Handle:FindFirstChild('PumpSound') then
					Handle.PumpSound:Play()
				end
				wait(.2)
			end
			Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunCursor.png")
		end		
		IsShooting = false
		if AmmoInClip == 0 then
			Reload()
		end
		if RecoilTrack then
			RecoilTrack:Stop()
		end
	end
end

function UpdateAmmo(value)
	if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
		WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
	end
	--if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then
	--	WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo
	--end
end


function OnMouseDown()
	LeftButtonDown = true
	OnFire()
end

function OnMouseUp()
	LeftButtonDown = false
end

function OnKeyDown(key)
	if string.lower(key) == 'r' then
		Reload()
	end
end

Tool.Input.OnServerEvent:connect(function(client, action, ...)
	if client.Character == Tool.Parent then
		if action == 'Mouse1' then
			local down, pos = ...
			
			if down then
				OnMouseDown()
			else
				OnMouseUp()
			end
		elseif action == 'Key' then
			local down, key = ...
			
			if down then
				OnKeyDown(key)
			end
		elseif action == 'MouseMove' then
			local pos = ...
			
			MOUSE_TARGET = pos
		end
	end
end)


function OnEquipped()
	EquipAnim = WaitForChild(Tool, 'Equip')
	PumpAnim = WaitForChild(Tool, 'Reload')
	FireSound  = WaitForChild(Handle, 'FireSound')

	MyCharacter = Tool.Parent
	MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter)
	MyHumanoid = MyCharacter:FindFirstChild('Humanoid')
	MyTorso = MyCharacter:FindFirstChild('Torso')
	WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone()
	if WeaponGui and MyPlayer then
		WeaponGui.Parent = MyPlayer.PlayerGui
		UpdateAmmo(AmmoInClip)
	end
	if EquipAnim then
		Tool.Grip = CFrame.new(0.132808685, 0.00749024749, 0.526850462, 0.925221086, 0.000443253666, 0.379427731, 0.00859767944, 0.999718785, -0.0221330915, -0.379331142, 0.0237402022, 0.924956799)
		EquipTrack = MyHumanoid:LoadAnimation(EquipAnim)
		EquipTrack:Play()
	end
	if PumpAnim then
		PumpTrack = MyHumanoid:LoadAnimation(PumpAnim)
	end

	Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunCursor.png")
end

