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()if MyMouse then MyMouse.Icon="\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 V=MyMouse.Hit.p for L=1,BulletCount,1 do local B=(V-Handle.Position).unit B=((CFrame.new(Vector3.new(0,0,0),B)*CFrame.Angles(0,0,(math.random()*2)*math.pi))*CFrame.Angles(math.random()*Spread,0,0)).lookVector local U,A=RayCast(Handle.Position,B,Range)local m if U then m=CreateBullet(A)end if U and U.Parent then local V=U.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if V then local L=game.Players:GetPlayerFromCharacter(V.Parent)if MyPlayer.Neutral or L and L.TeamColor~=MyPlayer.TeamColor then TagHumanoid(V,MyPlayer)V:TakeDamage(Damage)if m then m:Destroy()m=nil end end end end end local L=CasingBase:Clone()L.Position=Tool.Handle.Position+Vector3.new(0,1,0)L.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(L,2.5)L.Parent=game.Workspace AmmoInClip=AmmoInClip-1 UpdateAmmo(AmmoInClip)end 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 MyMouse.Icon="\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(V)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(V)if string.lower(V)=="\114"then Reload()end end function OnEquipped(V)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")MyMouse=V 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 if MyMouse then MyMouse.Icon="\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"MyMouse.Button1Down:connect(OnMouseDown)MyMouse.Button1Up:connect(OnMouseUp)MyMouse.KeyDown:connect(OnKeyDown)end end
--CasingMesh.Scale = Vector3.new(.75,.75,.75)
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()
			if MyMouse then
				MyMouse.Icon = "rbxasset://textures\\GunWaitCursor.png"
				local targetPoint = MyMouse.Hit.p
				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)
					end
					if hitObject and hitObject.Parent then
						local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid")
						if hitHumanoid then
							local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent)
							if MyPlayer.Neutral or (hitPlayer and hitPlayer.TeamColor ~= MyPlayer.TeamColor) then
								TagHumanoid(hitHumanoid, MyPlayer)
								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() - 0.5), (0.5 + math.random()), (math.random() - 0.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)
			end
			wait(FireRate - 0.2)

			if AmmoInClip > 0 then
				if PumpTrack then
					PumpTrack:Play()
				end
				if Handle:FindFirstChild("PumpSound") then
					Handle.PumpSound:Play()
				end
				wait(0.2)
			end
			MyMouse.Icon = "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

function OnEquipped(mouse)
	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")
	MyMouse = mouse
	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

	if MyMouse then
		MyMouse.Icon = "rbxasset://textures\\GunCursor.png"
		-- Disable mouse icon
		--MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
		MyMouse.Button1Down:connect(OnMouseDown)
		MyMouse.Button1Up:connect(OnMouseUp)
		MyMouse.KeyDown:connect(OnKeyDown)
	end
end
