local T=nil local E=nil local i=false local C=time()local W=script.Parent:WaitForChild("\070\108\097\114\101")local r=nil local w=Workspace.Terrain.WorldToCellPreferSolid local J=Workspace.Terrain.GetCell function RayIgnoreCheck(T,E)if T then if T.Transparency>=1 or string.lower(T.Name)=="\119\097\116\101\114"or T.Name=="\069\102\102\101\099\116"or T.Name=="\082\111\099\107\101\116"or T.Name=="\066\117\108\108\101\116"or T.Name=="\072\097\110\100\108\101"or T:IsDescendantOf(MyCharacter)then return true elseif T:IsA("\084\101\114\114\097\105\110")and E then local T=w(Workspace.Terrain,E)if T then local E=J(Workspace.Terrain,T.x,T.y,T.z)if E and E==Enum.CellMaterial.Water then return true end end end end return false end function RayCast(T,E,i)local C,W=game.Workspace:FindPartOnRay(Ray.new(T+E*.01,E*i),Handle)if C and W then local r=i-(W-T).magnitude if RayIgnoreCheck(C,W)and r>0 then return RayCast(W,E,r)end end return C,W end function TagHumanoid(T,E)while T:FindFirstChild("\099\114\101\097\116\111\114")do(T:FindFirstChild("\099\114\101\097\116\111\114")):Destroy()end local i=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")i.Value=E i.Name="\099\114\101\097\116\111\114"i.Parent=T DebrisService:AddItem(i,1.5)local C=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")C.Value=IconURL C.Name="\105\099\111\110"C.Parent=i end local function K(T)local E=Instance.new("\080\097\114\116",Workspace)E.FormFactor=Enum.FormFactor.Custom E.Size=Vector3.new(.1,.1,.1)E.BrickColor=BrickColor.new("\066\108\097\099\107")E.Shape=Enum.PartType.Block E.CanCollide=false E.CFrame=CFrame.new(T)E.Anchored=true E.TopSurface=Enum.SurfaceType.Smooth E.BottomSurface=Enum.SurfaceType.Smooth E.Name="\066\117\108\108\101\116"DebrisService:AddItem(E,2.5)local i=Instance.new("\080\097\114\116")i.CFrame=Tool.Handle.CFrame*CFrame.fromEulerAnglesXYZ(1.5,0,0)i.Size=Vector3.new(1,1,1)i.BrickColor=BrickColor.new(226)i.Parent=game.Workspace i.CFrame=script.Parent.Handle.CFrame i.CanCollide=false i.Transparency=0 i.BottomSurface=0 i.TopSurface=0 i.Name="\083\104\101\108\108"i.Velocity=Tool.Handle.CFrame.lookVector*35+Vector3.new(math.random(-10,10),20,math.random(-10,20))i.RotVelocity=Vector3.new(0,200,0)DebrisService:AddItem(i,1)local C=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")C.Scale=Vector3.new(.15,.4,.15)C.Parent=i return E end local function z()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 wait(ReloadTime)local T=math.min(ClipSize-AmmoInClip,SpareAmmo)AmmoInClip=AmmoInClip+T SpareAmmo=SpareAmmo-T 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 i then Spread=math.min(MaxSpread,Spread+AimInaccuracyStepAmount)UpdateCrosshair(Spread)end i=not i if Handle:FindFirstChild("\070\105\114\101\083\111\117\110\100")then W.MuzzleFlash.Enabled=true end if MyMouse then local T=MyMouse.Hit.p local E=(T-Handle.Position).unit E=CFrame.Angles(((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread)*E local i,C=RayCast(Handle.Position,E,Range)local W if i then W=K(C)end if i and i.Parent then local T=i.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if T then local E=game.Players:GetPlayerFromCharacter(T.Parent)if MyPlayer.Neutral or E and E.TeamColor~=MyPlayer.TeamColor then TagHumanoid(T,MyPlayer)script.Parent.Damager:FireServer(T,Damage)if W then W:Destroy()W=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 W.MuzzleFlash.Enabled=false if AmmoInClip==0 then z()end if RecoilTrack then RecoilTrack:Stop()end end end local d=0 function UpdateTargetHit()d=d+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)d=d-1 if d==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(T,E)if WeaponGui then local E=650 WeaponGui.Crosshair:TweenSize(UDim2.new(0,(T*E)*2+23,0,(T*E)*2+23),Enum.EasingDirection.Out,Enum.EasingStyle.Linear,.33)end end function UpdateAmmo(T)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 T>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(T)if string.lower(T)=="\114"then z()if RecoilTrack then RecoilTrack:Stop()end end end function OnEquipped(T)RecoilAnim=WaitForChild(Tool,"\082\101\099\111\105\108")ReloadAnim=WaitForChild(Tool,"\082\101\108\111\097\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=T 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 OnUnequipped()LeftButtonDown=false W.MuzzleFlash.Enabled=false Reloading=false MyCharacter=nil MyHumanoid=nil MyTorso=nil MyPlayer=nil MyMouse=nil if T then T:disconnect()end if E then E:disconnect()end if r then r=nil end if WeaponGui then WeaponGui.Parent=nil WeaponGui=nil end if RecoilTrack then RecoilTrack:Stop()end end local function y(T)if WeaponGui and WeaponGui:FindFirstChild("\067\114\111\115\115\104\097\105\114")then for E,i in pairs(WeaponGui.Crosshair:GetChildren())do if i:IsA("\070\114\097\109\101")then i.BorderColor3=T end end end end Tool.Equipped:connect(OnEquipped)Tool.Unequipped:connect(OnUnequipped)while true do wait(.033)if WeaponGui and(WeaponGui:FindFirstChild("\067\114\111\115\115\104\097\105\114")and MyMouse)then WeaponGui.Crosshair.Position=UDim2.new(0,MyMouse.X,0,MyMouse.Y)y(NeutralReticleColor)local T=MyMouse.Target if T and T.Parent then local E=PlayersService:GetPlayerFromCharacter(T.Parent)if E then if MyPlayer.Neutral or E.TeamColor~=MyPlayer.TeamColor then y(EnemyReticleColor)else y(FriendlyReticleColor)end end end end if Spread and not IsShooting then local T=time()if T-C>FireRate*2 then C=T Spread=math.max(MinSpread,Spread-AimInaccuracyStepAmount)UpdateCrosshair(Spread,MyMouse)end end end


local OnFireConnection = nil
local OnReloadConnection = nil

local DecreasedAimLastShot = false
local LastSpreadUpdate = time()

local flare = script.Parent:WaitForChild("Flare")


local FlashHolder = nil


local WorldToCellFunction = Workspace.Terrain.WorldToCellPreferSolid
local GetCellFunction = Workspace.Terrain.GetCell

function RayIgnoreCheck(hit, pos)
	if hit then
		if hit.Transparency >= 1 or string.lower(hit.Name) == "water" or
				hit.Name == "Effect" or hit.Name == "Rocket" or hit.Name == "Bullet" or
				hit.Name == "Handle" or hit:IsDescendantOf(MyCharacter) then
			return true
		elseif hit:IsA('Terrain') and pos then
			local cellPos = WorldToCellFunction(Workspace.Terrain, pos)
			if cellPos then
				local cellMat = GetCellFunction(Workspace.Terrain, cellPos.x, cellPos.y, cellPos.z)
				if cellMat and cellMat == Enum.CellMaterial.Water then
					return true
				end
			end
		end
	end
	return false
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

			return RayCast(hitPos, vec, distance)
		end
	end
	return hitObject, hitPos
end



function TagHumanoid(humanoid, player)

	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
	
		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)
			
			local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo)
			AmmoInClip = AmmoInClip + ammoToUse
			SpareAmmo = SpareAmmo - ammoToUse
			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('FireSound') then
				--Pitch.Pitch = .8 + (math.random() * .5)
				--Handle.FireSound:Play()
				--Handle.Flash.Enabled = true
				flare.MuzzleFlash.Enabled = true
			
			end
			if MyMouse then
				local targetPoint = MyMouse.Hit.p
				local shootDirection = (targetPoint - Handle.Position).unit
				
				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
				
				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)
							script.Parent.Damager:FireServer(hitHumanoid, Damage)
							--hitHumanoid:TakeDamage(Damage)
							if bullet then
								bullet:Destroy()
								bullet = 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()
			
			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
	
		MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154"
		MyMouse.Button1Down:connect(OnMouseDown)
		MyMouse.Button1Up:connect(OnMouseUp)
		MyMouse.KeyDown:connect(OnKeyDown)
	end
end



function OnUnequipped()
	--Handle.UnequipSound:Play()
	--Handle.EquipSound:Stop()
	--Handle.EquipSound2:Stop()
	LeftButtonDown = false
	flare.MuzzleFlash.Enabled = false
	Reloading = false
	MyCharacter = nil
	MyHumanoid = nil
	MyTorso = nil
	MyPlayer = nil
	MyMouse = nil
	if OnFireConnection then
		OnFireConnection:disconnect()
	end
	if OnReloadConnection then
		OnReloadConnection:disconnect()
	end
	if FlashHolder then
		FlashHolder = nil
	end
	if WeaponGui then
		WeaponGui.Parent = nil
		WeaponGui = nil
	end
	if RecoilTrack then
		RecoilTrack:Stop()
	end
	--if ReloadTrack then
		--ReloadTrack:Stop()
	--end
end

local function SetReticleColor(color)
	if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then
		for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do
			if line:IsA('Frame') then
				line.BorderColor3 = color
			end
		end
	end
end


Tool.Equipped:connect(OnEquipped)
Tool.Unequipped:connect(OnUnequipped)

while true do
	wait(0.033)
	if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then
		WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y)
		SetReticleColor(NeutralReticleColor)

		local target = MyMouse.Target
		if target and target.Parent then
			local player = PlayersService:GetPlayerFromCharacter(target.Parent)
			if player then
				if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then
					SetReticleColor(EnemyReticleColor)
				else
					SetReticleColor(FriendlyReticleColor)
				end
			end
		end
	end
	if Spread and not IsShooting then
		local currTime = time()
		if currTime - LastSpreadUpdate > FireRate * 2 then
			LastSpreadUpdate = currTime
			Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount)
			UpdateCrosshair(Spread, MyMouse)
		end
	end
end
