function RayCast(h,o,W)local Q,M=game.Workspace:FindPartOnRay(Ray.new(h+o*.01,o*W),Handle)if Q and M then local d=W-(M-h).magnitude if RayIgnoreCheck(Q,M)and d>0 then return RayCast(M,o,d)end end return Q,M end function TagHumanoid(h,o)while h:FindFirstChild("\099\114\101\097\116\111\114")do(h:FindFirstChild("\099\114\101\097\116\111\114")):Destroy()end local W=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")W.Value=o W.Name="\099\114\101\097\116\111\114"W.Parent=h DebrisService:AddItem(W,1.5)local Q=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")Q.Value=IconURL Q.Name="\105\099\111\110"Q.Parent=W end local function h(h)local o=Instance.new("\080\097\114\116",Workspace)o.FormFactor=Enum.FormFactor.Custom o.Size=Vector3.new(.1,.1,.1)o.BrickColor=BrickColor.new("\066\108\097\099\107")o.Shape=Enum.PartType.Block o.CanCollide=false o.CFrame=CFrame.new(h)o.Anchored=true o.TopSurface=Enum.SurfaceType.Smooth o.BottomSurface=Enum.SurfaceType.Smooth o.Name="\066\117\108\108\101\116"DebrisService:AddItem(o,2.5)local W=Instance.new("\080\097\114\116")W.CFrame=Tool.Handle.CFrame*CFrame.fromEulerAnglesXYZ(1.5,0,0)W.Size=Vector3.new(1,1,1)W.BrickColor=BrickColor.new(226)W.Parent=game.Workspace W.CFrame=script.Parent.Handle.CFrame W.CanCollide=false W.Transparency=0 W.BottomSurface=0 W.TopSurface=0 W.Name="\083\104\101\108\108"W.Velocity=Tool.Handle.CFrame.lookVector*35+Vector3.new(math.random(-10,10),20,math.random(-10,20))W.RotVelocity=Vector3.new(0,200,0)DebrisService:AddItem(W,1)local Q=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")Q.Scale=Vector3.new(.15,.4,.15)Q.Parent=W return o end local function o()if not Reloading then Reloading=true if AmmoInClip~=ClipSize and SpareAmmo>0 then if RecoilTrack then RecoilTrack:Stop()end if WeaponGui and WeaponGui:FindFirstChild("\067\114\111\115\115\104\097\105\114")then if WeaponGui.Crosshair:FindFirstChild("\082\101\108\111\097\100\105\110\103\076\097\098\101\108")then WeaponGui.Crosshair.ReloadingLabel.Visible=true end end if ReloadTrack then ReloadTrack:Play()end script.Parent.Handle.Reload:Play()wait(ReloadTime)local h=math.min(ClipSize-AmmoInClip,SpareAmmo)AmmoInClip=AmmoInClip+h SpareAmmo=SpareAmmo-h UpdateAmmo(AmmoInClip)if ReloadTrack then ReloadTrack:Stop()end end Reloading=false end end function OnFire()if IsShooting then return end if MyHumanoid and MyHumanoid.Health>0 then if RecoilTrack and AmmoInClip>0 then RecoilTrack:Play()end IsShooting=true while LeftButtonDown and(AmmoInClip>0 and not Reloading)do if Spread and not DecreasedAimLastShot then Spread=math.min(MaxSpread,Spread+AimInaccuracyStepAmount)UpdateCrosshair(Spread)end DecreasedAimLastShot=not DecreasedAimLastShot if Handle:FindFirstChild("\070\105\114\101\083\111\117\110\100")then Pitch.Pitch=.8+math.random()*.5 Handle.FireSound:Play()Handle.Flash.Enabled=true flare.MuzzleFlash.Enabled=true end if MyMouse then local o=MyMouse.Hit.p local W=(o-Handle.Position).unit W=CFrame.Angles(((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread)*W local Q,M=RayCast(Handle.Position,W,Range)local d if Q then d=h(M)end if Q and Q.Parent then local h=Q.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if h then local o=game.Players:GetPlayerFromCharacter(h.Parent)if MyPlayer.Neutral or o and o.TeamColor~=MyPlayer.TeamColor then TagHumanoid(h,MyPlayer)h:TakeDamage(Damage)if d then d:Destroy()d=nil WeaponGui.Crosshair.Hit:Play()end Spawn(UpdateTargetHit)end end end AmmoInClip=AmmoInClip-1 UpdateAmmo(AmmoInClip)end wait(FireRate)end Handle.Flash.Enabled=false IsShooting=false flare.MuzzleFlash.Enabled=false if AmmoInClip==0 then Handle.Tick:Play()o()end if RecoilTrack then RecoilTrack:Stop()end end end local W=0 function UpdateTargetHit()W=W+1 if WeaponGui and(WeaponGui:FindFirstChild("\067\114\111\115\115\104\097\105\114")and WeaponGui.Crosshair:FindFirstChild("\084\097\114\103\101\116\072\105\116\073\109\097\103\101"))then WeaponGui.Crosshair.TargetHitImage.Visible=true end wait(.5)W=W-1 if W==0 and(WeaponGui and(WeaponGui:FindFirstChild("\067\114\111\115\115\104\097\105\114")and WeaponGui.Crosshair:FindFirstChild("\084\097\114\103\101\116\072\105\116\073\109\097\103\101")))then WeaponGui.Crosshair.TargetHitImage.Visible=false end end function UpdateCrosshair(h,o)if WeaponGui then local o=650 WeaponGui.Crosshair:TweenSize(UDim2.new(0,(h*o)*2+23,0,(h*o)*2+23),Enum.EasingDirection.Out,Enum.EasingStyle.Linear,.33)end end function UpdateAmmo(h)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 if h>0 and(WeaponGui:FindFirstChild("\067\114\111\115\115\104\097\105\114")and WeaponGui.Crosshair:FindFirstChild("\082\101\108\111\097\100\105\110\103\076\097\098\101\108"))then WeaponGui.Crosshair.ReloadingLabel.Visible=false end end if WeaponGui and(WeaponGui:FindFirstChild("\065\109\109\111\072\117\100")and WeaponGui.AmmoHud:FindFirstChild("\084\111\116\097\108\065\109\109\111"))then WeaponGui.AmmoHud.TotalAmmo.Text=SpareAmmo end end function OnMouseDown()LeftButtonDown=true OnFire()end function OnMouseUp()LeftButtonDown=false end function OnKeyDown(h)if string.lower(h)=="\114"then o()if RecoilTrack then RecoilTrack:Stop()end end end function OnEquipped(h)Handle.EquipSound:Play()Handle.EquipSound2:Play()Handle.UnequipSound:Stop()RecoilAnim=WaitForChild(Tool,"\082\101\099\111\105\108")ReloadAnim=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=h 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 RecoilAnim then RecoilTrack=MyHumanoid:LoadAnimation(RecoilAnim)end if ReloadAnim then ReloadTrack=MyHumanoid:LoadAnimation(ReloadAnim)end if MyMouse then MyMouse.Icon="\104\116\116\112\058\047\047\119\119\119\046\114\111\098\108\111\120\046\099\111\109\047\097\115\115\101\116\047\063\105\100\061\049\056\054\054\050\049\053\052"MyMouse.Button1Down:connect(OnMouseDown)MyMouse.Button1Up:connect(OnMouseUp)MyMouse.KeyDown:connect(OnKeyDown)end end

function RayCast(startPos, vec, rayLength)
	local hitObject, hitPos = game.Workspace:FindPartOnRay(Ray.new(startPos + (vec * .01), vec * rayLength), Handle)
	if hitObject and hitPos then
		local distance = rayLength - (hitPos - startPos).magnitude
		if RayIgnoreCheck(hitObject, hitPos) and distance > 0 then
			-- there is a chance here for potential infinite recursion
			return RayCast(hitPos, vec, distance)
		end
	end
	return hitObject, hitPos
end



function TagHumanoid(humanoid, player)
	-- Add more tags here to customize what tags are available.
	while humanoid:FindFirstChild('creator') do
		humanoid:FindFirstChild('creator'):Destroy()
	end 
	local creatorTag = Instance.new("ObjectValue")
	creatorTag.Value = player
	creatorTag.Name = "creator"
	creatorTag.Parent = humanoid
	DebrisService:AddItem(creatorTag, 1.5)

	local weaponIconTag = Instance.new("StringValue")
	weaponIconTag.Value = IconURL
	weaponIconTag.Name = "icon"
	weaponIconTag.Parent = creatorTag
end


local function CreateBullet(bulletPos)
	local bullet = Instance.new('Part', Workspace)
	bullet.FormFactor = Enum.FormFactor.Custom
	bullet.Size = Vector3.new(0.1, 0.1, 0.1)
	bullet.BrickColor = BrickColor.new("Black")
	bullet.Shape = Enum.PartType.Block
	bullet.CanCollide = false
	bullet.CFrame = CFrame.new(bulletPos)
	bullet.Anchored = true
	bullet.TopSurface = Enum.SurfaceType.Smooth
	bullet.BottomSurface = Enum.SurfaceType.Smooth
	bullet.Name = 'Bullet'
	DebrisService:AddItem(bullet, 2.5)
	
	local shell = Instance.new("Part")
	shell.CFrame = Tool.Handle.CFrame * CFrame.fromEulerAnglesXYZ(1.5,0,0)
	shell.Size = Vector3.new(1,1,1)
	shell.BrickColor = BrickColor.new(226)
	shell.Parent = game.Workspace
	shell.CFrame = script.Parent.Handle.CFrame
	shell.CanCollide = false
	shell.Transparency = 0
	shell.BottomSurface = 0
	shell.TopSurface = 0
	shell.Name = "Shell"
	shell.Velocity = Tool.Handle.CFrame.lookVector * 35 + Vector3.new(math.random(-10,10),20,math.random(-10,20))
	shell.RotVelocity = Vector3.new(0,200,0)
	DebrisService:AddItem(shell, 1)

	local shellmesh = Instance.new("SpecialMesh")
	shellmesh.Scale = Vector3.new(.15,.4,.15)
	shellmesh.Parent = shell
	
	return bullet
end

local function Reload()
	if not Reloading then
		Reloading = true
		-- Don't reload if you are already full or have no extra ammo
		if AmmoInClip ~= ClipSize and SpareAmmo > 0 then
			if RecoilTrack then
				RecoilTrack:Stop()
			end
			if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
				if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
					WeaponGui.Crosshair.ReloadingLabel.Visible = true
				end
			end
			if ReloadTrack then
				ReloadTrack:Play()
			end
			script.Parent.Handle.Reload:Play()
			wait(ReloadTime)
			-- Only use as much ammo as you have
			local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo)
			AmmoInClip = AmmoInClip + ammoToUse
			SpareAmmo = SpareAmmo - ammoToUse
			UpdateAmmo(AmmoInClip)
			--WeaponGui.Reload.Visible = false
			if ReloadTrack then
				ReloadTrack:Stop()
			end
		end
		Reloading = false
	end
end

function OnFire()
	if IsShooting then return end
	if MyHumanoid and MyHumanoid.Health > 0 then
		if RecoilTrack and AmmoInClip > 0 then
			RecoilTrack:Play()
		end
		IsShooting = true
		while LeftButtonDown and AmmoInClip > 0 and not Reloading do
			if Spread and not DecreasedAimLastShot then
				Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount)
				UpdateCrosshair(Spread)
			end
			DecreasedAimLastShot = not DecreasedAimLastShot
			if Handle:FindFirstChild('FireSound') then
				Pitch.Pitch = .8 + (math.random() * .5)
				Handle.FireSound:Play()
				Handle.Flash.Enabled = true
				flare.MuzzleFlash.Enabled = true
				--Handle.Smoke.Enabled=true --This is optional
			end
			if MyMouse then
				local targetPoint = MyMouse.Hit.p
				local shootDirection = (targetPoint - Handle.Position).unit
				-- Adjust the shoot direction randomly off by a little bit to account for recoil
				shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread,
																(0.5 - math.random()) * 2 * Spread) * shootDirection
				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)
							--game.ReplicatedStorage.Remotes.TakeDamage:FireServer(hitHumanoid)
							if bullet then
								bullet:Destroy()
								bullet = nil
								WeaponGui.Crosshair.Hit:Play()
								--bullet.Transparency = 1
							end
							Spawn(UpdateTargetHit)
						end
					end
				end
				AmmoInClip = AmmoInClip - 1
				UpdateAmmo(AmmoInClip)
			end
			wait(FireRate)
		end
		Handle.Flash.Enabled = false
		IsShooting = false
		flare.MuzzleFlash.Enabled = false
		--Handle.Smoke.Enabled=false --This is optional
		if AmmoInClip == 0 then
			Handle.Tick:Play()
			--WeaponGui.Reload.Visible = true
			Reload()
		end
		if RecoilTrack then
			RecoilTrack:Stop()
		end
	end
end

local TargetHits = 0
function UpdateTargetHit()
	TargetHits = TargetHits + 1
	if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
		WeaponGui.Crosshair.TargetHitImage.Visible = true
	end
	wait(0.5)
	TargetHits = TargetHits - 1
	if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then
		WeaponGui.Crosshair.TargetHitImage.Visible = false
	end
end

function UpdateCrosshair(value, mouse)
	if WeaponGui then
		local absoluteY = 650
		WeaponGui.Crosshair:TweenSize(
			UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23),
			Enum.EasingDirection.Out,
			Enum.EasingStyle.Linear,
			0.33)
	end
end

function UpdateAmmo(value)
	if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then
		WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip
		if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then
			WeaponGui.Crosshair.ReloadingLabel.Visible = false
		end
	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()
		if RecoilTrack then
			RecoilTrack:Stop()
		end
	end
end


function OnEquipped(mouse)
	Handle.EquipSound:Play()
	Handle.EquipSound2:Play()
	Handle.UnequipSound:Stop()
	RecoilAnim = WaitForChild(Tool, 'Recoil')
	ReloadAnim = 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 RecoilAnim then
		RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim)
	end
	
	if ReloadAnim then
		ReloadTrack = MyHumanoid:LoadAnimation(ReloadAnim)
	end

	if MyMouse then
		-- 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

