local function P(P)local Y=P:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if Y then if Y.RigType==Enum.HumanoidRigType.R15 then return"\082\049\053"elseif Y.RigType==Enum.HumanoidRigType.R6 then return"\082\054"end end end function RayCast(P,Y,r)local n=P local S=Y*r local D=RaycastParams.new()D.FilterType=Enum.RaycastFilterType.Blacklist D.IgnoreWater=true D.FilterDescendantsInstances={Tool.Parent;workspace.BulletsFolder;Tool}local Q=workspace:Raycast(n,S,D)if Q then return Q.Instance,Q.Position end end local function Y()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()task.wait(ReloadTime)local P=math.min(ClipSize-AmmoInClip,SpareAmmo)AmmoInClip=AmmoInClip+P SpareAmmo=SpareAmmo-P 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 P=MyMouse.Hit.Position local Y=(P-Handle.Position).unit Y=CFrame.Angles(((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread)*Y local r,n=RayCast(Handle.Position,Y,Range)local S if r then bulletRE:FireServer(n)WeaponGui.Crosshair.Hit:Play()end if r and r.Parent then local P=r.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if P then local Y=game.Players:GetPlayerFromCharacter(P.Parent)if MyPlayer.Neutral or Y and Y.TeamColor~=MyPlayer.TeamColor then ShootRE:FireServer(P,Damage)if S then S:Destroy()S=nil end task.spawn(UpdateTargetHit)end end end AmmoInClip=AmmoInClip-1 UpdateAmmo(AmmoInClip)end task.wait(FireRate)end Handle.Flash.Enabled=false IsShooting=false flare.MuzzleFlash.Enabled=false if AmmoInClip==0 then Handle.Tick:Play()Y()end if RecoilTrack then RecoilTrack:Stop()end end end local r=0 function UpdateTargetHit()r=r+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 task.wait(.5)r=r-1 if r==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(P,Y)if WeaponGui then local Y=650 WeaponGui.Crosshair:TweenSize(UDim2.new(0,(P*Y)*2+23,0,(P*Y)*2+23),Enum.EasingDirection.Out,Enum.EasingStyle.Linear,.33)end end function UpdateAmmo(P)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 P>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(P)if string.lower(P)=="\114"then Y()if RecoilTrack then RecoilTrack:Stop()end end end local n={}function OnEquipped(Y)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=Y WeaponGui=(WaitForChild(Tool,"\087\101\097\112\111\110\072\117\100")):Clone()if FPSHandsEnbled then if AutoFirstPerson then MyPlayer.CameraMode=Enum.CameraMode.LockFirstPerson end local Y=P(MyCharacter)if Y=="\082\049\053"then local P={"\076\101\102\116\072\097\110\100","\076\101\102\116\076\111\119\101\114\065\114\109";"\076\101\102\116\085\112\112\101\114\065\114\109","\082\105\103\104\116\072\097\110\100","\082\105\103\104\116\076\111\119\101\114\065\114\109","\082\105\103\104\116\085\112\112\101\114\065\114\109"}for Y,r in pairs(MyCharacter:GetDescendants())do if table.find(P,r.Name)and r:IsA("\066\097\115\101\080\097\114\116")then r.LocalTransparencyModifier=r.Transparency n[r.Name]=(r:GetPropertyChangedSignal("\076\111\099\097\108\084\114\097\110\115\112\097\114\101\110\099\121\077\111\100\105\102\105\101\114")):Connect(function()r.LocalTransparencyModifier=r.Transparency end)end end elseif Y=="\082\054"then local P={"\082\105\103\104\116 \065\114\109","\076\101\102\116 \065\114\109"}for Y,r in pairs(MyCharacter:GetDescendants())do if table.find(P,r.Name)and r:IsA("\066\097\115\101\080\097\114\116")then r.LocalTransparencyModifier=r.Transparency n[r.Name]=(r:GetPropertyChangedSignal("\076\111\099\097\108\084\114\097\110\115\112\097\114\101\110\099\121\077\111\100\105\102\105\101\114")):Connect(function()r.LocalTransparencyModifier=r.Transparency end)end end end end 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

local function DetectRigAsync(Char)
	local Hum = Char:FindFirstChildOfClass("Humanoid")

	if Hum then
		if Hum.RigType == Enum.HumanoidRigType.R15 then
			return "R15"
		elseif Hum.RigType == Enum.HumanoidRigType.R6 then
			return "R6"
		end
	end
end

function RayCast(startPos, vec, rayLength)
	local Origin = startPos

	local Direction = vec * rayLength

	local Params = RaycastParams.new()
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.IgnoreWater = true
	Params.FilterDescendantsInstances = {Tool.Parent, workspace.BulletsFolder, Tool}

	local raycastResult = workspace:Raycast(Origin, Direction, Params)

	if raycastResult then
		return raycastResult.Instance, raycastResult.Position
	end
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()
			task.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.Position
				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
					bulletRE:FireServer(bulletPos)
					WeaponGui.Crosshair.Hit:Play()
				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
							ShootRE:FireServer(hitHumanoid, Damage)
							if bullet then
								bullet:Destroy()
								bullet = nil
								--bullet.Transparency = 1
							end
							task.spawn(UpdateTargetHit)
						end
					end
				end
				AmmoInClip = AmmoInClip - 1
				UpdateAmmo(AmmoInClip)
			end
			task.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
	task.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

local TransparencyConnections = {}


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 FPSHandsEnbled then
		if AutoFirstPerson then
			MyPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
		end

		local Rig = DetectRigAsync(MyCharacter)

		if Rig == "R15" then
			local WhitelistedBodyParts = {"LeftHand", "LeftLowerArm", "LeftUpperArm", "RightHand", "RightLowerArm", "RightUpperArm"}

			for i, bodypart in pairs(MyCharacter:GetDescendants()) do
				if table.find(WhitelistedBodyParts, bodypart.Name) and bodypart:IsA("BasePart") then
					bodypart.LocalTransparencyModifier = bodypart.Transparency

					TransparencyConnections[bodypart.Name] = bodypart:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
						bodypart.LocalTransparencyModifier = bodypart.Transparency
					end)
				end
			end
		elseif Rig == "R6" then
			local WhitelistedBodyParts = {"Right Arm", "Left Arm"}
			
			for i, bodypart in pairs(MyCharacter:GetDescendants()) do
				if table.find(WhitelistedBodyParts, bodypart.Name) and bodypart:IsA("BasePart") then
					bodypart.LocalTransparencyModifier = bodypart.Transparency

					TransparencyConnections[bodypart.Name] = bodypart:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
						bodypart.LocalTransparencyModifier = bodypart.Transparency
					end)
				end
			end
			
		end
	end

	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

