function RayCast(n,U,G)local y,h=game.Workspace:FindPartOnRay(Ray.new(n+U*.01,U*G),Handle)if y and h then local d=G-(h-n).magnitude if RayIgnoreCheck(y,h)and d>0 then return RayCast(h,U,d)end end return y,h end function TagHumanoid(n,U)while n:FindFirstChild("\099\114\101\097\116\111\114")do(n:FindFirstChild("\099\114\101\097\116\111\114")):Destroy()end local G=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")G.Value=U G.Name="\099\114\101\097\116\111\114"G.Parent=n DebrisService:AddItem(G,1)end local function n()if not FlashHolder then FlashHolder=Instance.new("\080\097\114\116",Tool)FlashHolder.Name="\070\108\097\115\104\072\111\108\100\101\114"FlashHolder.Transparency=1 FlashHolder.CanCollide=false FlashHolder.FormFactor="\067\117\115\116\111\109"FlashHolder.Size=Vector3.new(.4,.4,.4)FlashHolder.Position=Tool.Handle.Position local n=Instance.new("\077\097\110\117\097\108\087\101\108\100")n.C0=CFrame.new(0,0,0,1,0,0,0,1,0,0,0,1)n.C1=CFrame.new(0,.4,-2.4,0,0,1,1,0,0,0,1,0)n.Part0=FlashHolder n.Part1=Tool.Handle n.Parent=FlashHolder end local n=Instance.new("\070\105\114\101",FlashHolder)n.Color=Color3.new(1,.54901960784314,0)n.SecondaryColor=Color3.new(1,0,0)n.Size=.3 DebrisService:AddItem(n,FireRate/1.5)end local U=Instance.new("\080\097\114\116")U.FormFactor=Enum.FormFactor.Custom U.Size=Vector3.new(1,1,1)U.BrickColor=BrickColor.new("\066\108\097\099\107")U.CanCollide=false U.Anchored=true U.TopSurface=Enum.SurfaceType.Smooth U.BottomSurface=Enum.SurfaceType.Smooth U.Name="\066\117\108\108\101\116"local G=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")G.MeshType="\083\112\104\101\114\101"G.Scale=Vector3.new(.2,.2,.2)G.Parent=U local function y(n,G)local y=U:Clone()y.CFrame=CFrame.new(n)y.Parent=game.Workspace DebrisService:AddItem(y,2.5)if G.Anchored==false then local U=Instance.new("\087\101\108\100")U.C0=G.CFrame:toObjectSpace(CFrame.new(n))U.Part0=G U.Part1=y U.Parent=y y.Anchored=false end return y end local function h()if not Reloading then Reloading=true Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent),"\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")if AmmoInClip~=ClipSize then if PumpTrack then PumpTrack:Stop()end for n=1,6,1 do if PumpTrack then PumpTrack:Play()end if Handle:FindFirstChild("\080\117\109\112\083\111\117\110\100")then Handle.PumpSound:Play()end wait(ReloadTime/6)end local n=ClipSize-AmmoInClip AmmoInClip=AmmoInClip+n UpdateAmmo(AmmoInClip)end Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent),"\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")Reloading=false end end local d=Instance.new("\080\097\114\116")d.FormFactor=Enum.FormFactor.Custom d.Elasticity=0 d.Size=Vector3.new(.3,.3,.5)local q=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")q.MeshId="\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\057\052\050\052\056\049\050\052"q.TextureId="\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\057\052\050\049\057\052\055\048"
-- @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)
end

local function CreateFlash()
	if not FlashHolder then
		FlashHolder = Instance.new("Part", Tool)
		FlashHolder.Name = "FlashHolder"
		FlashHolder.Transparency = 1
		FlashHolder.CanCollide = false
		FlashHolder.FormFactor = "Custom"
		FlashHolder.Size = Vector3.new(0.4, 0.4, 0.4)
		FlashHolder.Position = Tool.Handle.Position
		local Weld = Instance.new("ManualWeld")
		Weld.C0 = CFrame.new(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
		Weld.C1 = CFrame.new(0, 0.4, -2.4, 0, 0, 1, 1, 0, 0, 0, 1, 0)
		Weld.Part0 = FlashHolder
		Weld.Part1 = Tool.Handle
		Weld.Parent = FlashHolder
	end
	local flash = Instance.new("Fire", FlashHolder)
	flash.Color = Color3.new(1, 140 / 255, 0)
	flash.SecondaryColor = Color3.new(1, 0, 0)
	flash.Size = 0.3
	DebrisService:AddItem(flash, FireRate / 1.5)
end

local BulletBase = Instance.new("Part")
BulletBase.FormFactor = Enum.FormFactor.Custom
BulletBase.Size = Vector3.new(1, 1, 1)
BulletBase.BrickColor = BrickColor.new("Black")
BulletBase.CanCollide = false
BulletBase.Anchored = true
BulletBase.TopSurface = Enum.SurfaceType.Smooth
BulletBase.BottomSurface = Enum.SurfaceType.Smooth
BulletBase.Name = "Bullet"

local BulletMesh = Instance.new("SpecialMesh")
BulletMesh.MeshType = "Sphere"
BulletMesh.Scale = Vector3.new(0.2, 0.2, 0.2)
BulletMesh.Parent = BulletBase

local function CreateBullet(bulletPos, hit)
	local bullet = BulletBase:Clone()

	bullet.CFrame = CFrame.new(bulletPos)
	bullet.Parent = game.Workspace
	DebrisService:AddItem(bullet, 2.5)

	if hit.Anchored == false then
		local weld = Instance.new("Weld")
		weld.C0 = hit.CFrame:toObjectSpace(CFrame.new(bulletPos))
		weld.Part0 = hit
		weld.Part1 = bullet
		weld.Parent = bullet
		bullet.Anchored = false
	end

	return bullet
end

local function Reload()
	if not Reloading then
		Reloading = true
		Tool.SetIcon:FireClient(
			game.Players:GetPlayerFromCharacter(Tool.Parent),
			"rbxasset://textures\\GunWaitCursor.png"
		)
		-- Don't reload if you are already full or have no extra ammo
		if AmmoInClip ~= ClipSize then
			if PumpTrack then
				PumpTrack:Stop()
			end
			for i = 1, 6 do
				if PumpTrack then
					PumpTrack:Play()
				end
				if Handle:FindFirstChild("PumpSound") then
					Handle.PumpSound:Play()
				end
				wait(ReloadTime / 6)
			end
			-- Only use as much ammo as you have
			local ammoToUse = ClipSize - AmmoInClip
			AmmoInClip = AmmoInClip + ammoToUse
			UpdateAmmo(AmmoInClip)
		end
		Tool.SetIcon:FireClient(game.Players:GetPlayerFromCharacter(Tool.Parent), "rbxasset://textures\\GunCursor.png")
		Reloading = false
	end
end

local CasingBase = Instance.new("Part")
CasingBase.FormFactor = Enum.FormFactor.Custom
CasingBase.Elasticity = 0
CasingBase.Size = Vector3.new(0.3, 0.3, 0.5)
local CasingMesh = Instance.new("SpecialMesh")
CasingMesh.MeshId = "http://www.roblox.com/asset/?id=94248124"
CasingMesh.TextureId = "http://www.roblox.com/asset/?id=94219470"
