-- @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 * .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'