function RayCast(g,T,X)local E,a=game.Workspace:FindPartOnRay(Ray.new(g+T*.01,T*X),Handle)if E and a then local s=X-(a-g).magnitude if RayIgnoreCheck(E,a)and s>0 then return RayCast(a,T,s)end end return E,a end function TagHumanoid(g,T)while g:FindFirstChild("\099\114\101\097\116\111\114")do(g:FindFirstChild("\099\114\101\097\116\111\114")):Destroy()end local X=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")X.Value=T X.Name="\099\114\101\097\116\111\114"X.Parent=g DebrisService:AddItem(X,1.5)local E=Instance.new("\083\116\114\105\110\103\086\097\108\117\101")E.Value=IconURL E.Name="\105\099\111\110"E.Parent=X end local function g(g)local T=Instance.new("\080\097\114\116",Workspace)T.FormFactor=Enum.FormFactor.Custom T.Size=Vector3.new(.1,.1,.1)T.BrickColor=BrickColor.new("\066\108\097\099\107")T.Shape=Enum.PartType.Block T.CanCollide=false T.CFrame=CFrame.new(g)T.Anchored=true T.TopSurface=Enum.SurfaceType.Smooth T.BottomSurface=Enum.SurfaceType.Smooth T.Name="\066\117\108\108\101\116"DebrisService:AddItem(T,2.5)return T end local function T()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 for g=1,math.min(ClipSize-AmmoInClip,SpareAmmo),1 do Receiver:FireServer("\080\108\097\121\083\111\117\110\100","\082\101\108\111\097\100")wait(.5)end Receiver:FireServer("\080\108\097\121\083\111\117\110\100","\080\117\109\112\083\111\117\110\100")local g=math.min(ClipSize-AmmoInClip,SpareAmmo)AmmoInClip=AmmoInClip+g SpareAmmo=SpareAmmo-g UpdateAmmo(AmmoInClip)WeaponGui.Reload.Visible=false 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 script.Parent.Flash.Image.Enabled=true script.Parent.Flash.ImageTwo.Enabled=true script.Parent.Flash.Image.Flash.Rotation=rot[math.random(1,#rot)]script.Parent.Flash.ImageTwo.Flash.Rotation=rot[math.random(1,#rot)]script.Parent.Flash.Embers.Enabled=true if Handle:FindFirstChild("\070\105\114\101\083\111\117\110\100")then Receiver:FireServer("\080\108\097\121\083\111\117\110\100","\070\105\114\101\083\111\117\110\100")Receiver:FireServer("\070\108\097\115\104",true)end if MyMouse then for T=1,12,1 do local X=MyMouse.Hit.p local E=(X-Handle.Position).unit E=CFrame.Angles(((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread,((.5-math.random())*2)*Spread)*E local a,s=RayCast(Handle.Position,E,Range)local c if a then c=g(s)end if a and a.Parent then local g=a.Parent:FindFirstChild("\072\117\109\097\110\111\105\100")if g then local T=game.Players:GetPlayerFromCharacter(g.Parent)if MyPlayer.Neutral or T and T.TeamColor~=MyPlayer.TeamColor then TagHumanoid(g,MyPlayer)Receiver:FireServer("\068\097\109\097\103\101",g,Damage)if c then c:Destroy()c=nil end Spawn(UpdateTargetHit)elseif not T then TagHumanoid(g,MyPlayer)Receiver:FireServer("\068\097\109\097\103\101",g,Damage)if c then c:Destroy()c=nil end Spawn(UpdateTargetHit)end end end end AmmoInClip=AmmoInClip-1 UpdateAmmo(AmmoInClip)end Receiver:FireServer("\080\108\097\121\083\111\117\110\100","\080\117\109\112\083\111\117\110\100")wait(.2)Receiver:FireServer("\070\108\097\115\104",false)script.Parent.Flash.Image.Enabled=false script.Parent.Flash.ImageTwo.Enabled=false script.Parent.Flash.Embers.Enabled=false wait(FireRate)OnMouseUp()end IsShooting=false if AmmoInClip==0 then T()end if RecoilTrack then RecoilTrack:Stop()end end end local X=0 function UpdateTargetHit()X=X+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)X=X-1 if X==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(g,T)if WeaponGui then local T=650 WeaponGui.Crosshair:TweenSize(UDim2.new(0,(g*T)*2+23,0,(g*T)*2+23),Enum.EasingDirection.Out,Enum.EasingStyle.Linear,.33)end end function UpdateAmmo(g)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 g>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(g)if string.lower(g)=="\114"then T()end end function OnEquipped(g)Receiver:FireServer("\080\108\097\121\083\111\117\110\100","\069\113\117\105\112\083\111\117\110\100")RecoilAnim=WaitForChild(Tool,"\082\101\099\111\105\108")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=g 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 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
-- @preconditions: vec should be a unit vector, and 0 < rayLength <= 1000
function RayCast(startPos, vec, rayLength)
	local hitObject, hitPos = game.Workspace:FindPartOnRay(Ray.new(startPos + (vec * 0.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)
	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
			for i = 1, math.min(ClipSize - AmmoInClip, SpareAmmo) do
				Receiver:FireServer("PlaySound", "Reload")
				wait(0.5)
			end
			Receiver:FireServer("PlaySound", "PumpSound")
			-- 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
		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
			script.Parent.Flash.Image.Enabled = true
			script.Parent.Flash.ImageTwo.Enabled = true
			script.Parent.Flash.Image.Flash.Rotation = rot[math.random(1, #rot)]
			script.Parent.Flash.ImageTwo.Flash.Rotation = rot[math.random(1, #rot)]
			script.Parent.Flash.Embers.Enabled = true
			if Handle:FindFirstChild("FireSound") then
				Receiver:FireServer("PlaySound", "FireSound")
				Receiver:FireServer("Flash", true)
			end
			if MyMouse then
				for i = 1, 12 do -- Shotgun effect :P
					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)
								Receiver:FireServer("Damage", hitHumanoid, Damage)
								if bullet then
									bullet:Destroy()
									bullet = nil
									--bullet.Transparency = 1
								end
								Spawn(UpdateTargetHit)
							elseif not hitPlayer then
								TagHumanoid(hitHumanoid, MyPlayer)
								Receiver:FireServer("Damage", hitHumanoid, Damage)
								if bullet then
									bullet:Destroy()
									bullet = nil
									--bullet.Transparency = 1
								end
								Spawn(UpdateTargetHit)
							end
						end
					end
				end
				AmmoInClip = AmmoInClip - 1
				UpdateAmmo(AmmoInClip)
			end
			Receiver:FireServer("PlaySound", "PumpSound")
			wait(0.2)
			Receiver:FireServer("Flash", false)
			script.Parent.Flash.Image.Enabled = false
			script.Parent.Flash.ImageTwo.Enabled = false
			script.Parent.Flash.Embers.Enabled = false
			wait(FireRate)
			OnMouseUp()
		end
		IsShooting = false
		if AmmoInClip == 0 then
			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()
	end
end

function OnEquipped(mouse)
	Receiver:FireServer("PlaySound", "EquipSound")
	RecoilAnim = WaitForChild(Tool, "Recoil")
	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 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
