function RayCast(Y,p,r)local b,h=game.Workspace:FindPartOnRay(Ray.new(Y+p*.01,p*r),Handle)if b and h then local D=r-(h-Y).magnitude if RayIgnoreCheck(b,h)and D>0 then return RayCast(h,p,D)end end return b,h end function TagHumanoid(Y,p)while Y:FindFirstChild("\099\114\101\097\116\111\114")do(Y:FindFirstChild("\099\114\101\097\116\111\114")):Destroy()end local r=Instance.new("\079\098\106\101\099\116\086\097\108\117\101")r.Value=p r.Name="\099\114\101\097\116\111\114"r.Parent=Y DebrisService:AddItem(r,1)end local function Y()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 Y=Instance.new("\077\097\110\117\097\108\087\101\108\100")Y.C0=CFrame.new(0,0,0,1,0,0,0,1,0,0,0,1)Y.C1=CFrame.new(0,.4,-2.4,0,0,1,1,0,0,0,1,0)Y.Part0=FlashHolder Y.Part1=Tool.Handle Y.Parent=FlashHolder end local Y=Instance.new("\070\105\114\101",FlashHolder)Y.Color=Color3.new(1,.54901960784314,0)Y.SecondaryColor=Color3.new(1,0,0)Y.Size=.3 DebrisService:AddItem(Y,FireRate/1.5)end local p=Instance.new("\080\097\114\116")p.FormFactor=Enum.FormFactor.Custom p.Size=Vector3.new(1,1,1)p.BrickColor=BrickColor.new("\066\108\097\099\107")p.CanCollide=false p.Anchored=true p.TopSurface=Enum.SurfaceType.Smooth p.BottomSurface=Enum.SurfaceType.Smooth p.Name="\066\117\108\108\101\116"local r=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")r.MeshType="\083\112\104\101\114\101"r.Scale=Vector3.new(.2,.2,.2)r.Parent=p local function b(Y)local r=p:Clone()r.CFrame=CFrame.new(Y)r.Parent=game.Workspace DebrisService:AddItem(r,2.5)return r end local function h()if not Reloading then Reloading=true MyMouse.Icon="\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 Y=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 Y=ClipSize-AmmoInClip AmmoInClip=AmmoInClip+Y UpdateAmmo(AmmoInClip)end MyMouse.Icon="\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 g=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")g.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"g.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"

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)
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(.4,.4,.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, .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(.2,.2,.2)
BulletMesh.Parent = BulletBase


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

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

	return bullet
end

local function Reload()
	if not Reloading then
		Reloading = true
		MyMouse.Icon = "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
		MyMouse.Icon = "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(.3,.3,.5)
local CasingMesh = Instance.new('SpecialMesh')
CasingMesh.MeshId = 'http://www.roblox.com/asset/?id=94248124'
CasingMesh.TextureId = 'http://www.roblox.com/asset/?id=94219470'