function removeElement(g,N)for V,m in pairs(g)do if m==N then table.remove(g,V)break end end return g end function findFirstClass(g,N)local V=nil for g,m in pairs(g:GetChildren())do if m.ClassName==N then V=m break end end return V end function isIgnored(g,N)for N,V in pairs(N)do if g==V or g:IsDescendantOf(V)then return true end end return false end function GetHitSurfaceCFrame(g,N)local V={{"\066\097\099\107";N.CFrame*CF(0,0,N.Size.z)},{"\066\111\116\116\111\109";N.CFrame*CF(0,-N.Size.y,0)},{"\070\114\111\110\116",N.CFrame*CF(0,0,-N.Size.z)};{"\076\101\102\116",N.CFrame*CF(-N.Size.x,0,0)},{"\082\105\103\104\116";N.CFrame*CF(N.Size.x,0,0)};{"\084\111\112";N.CFrame*CF(0,N.Size.y,0)}}local m=HUGE local a=nil for N,V in pairs(V)do local z=(g-V[2].p).magnitude if z<m then m=z a=V end end return a[2]end function AdvRayCast(g,N,V,m)local a=m and m or Ignore local z=Ray.new(g,N*(V>999 and 999 or V))local O,s=game.Workspace:FindPartOnRayWithIgnoreList(z,a)local e=s local t,Y=nil,nil local o=math.floor(V/999)if not O and V>999 then for m=0,o,1 do local z=m==o and V-(e-g).magnitude or 999 local O=Ray.new(e,N*z)local s,y=game.Workspace:FindPartOnRayWithIgnoreList(O,a)if m~=o then if s then t,Y=s,y break end elseif m==o then t,Y=s,y end e=y end return t,Y elseif O or V<=999 then return O,s end end


function removeElement(Table, Element) --removes the first instance of Element from Table
	for i, v in pairs(Table) do
		if v == Element then
			table.remove(Table, i)
			break
		end
	end
	return Table
end

function findFirstClass(Object, Class)
	local foundObject = nil
	for _, Obj in pairs(Object:GetChildren()) do
		if Obj.ClassName == Class then
			foundObject = Obj
			break
		end
	end
	return foundObject
end

function isIgnored(Obj, Table)
	for _,v in pairs(Table) do
		if Obj == v or Obj:IsDescendantOf(v) then
			return true
		end
	end
	return false
end

function GetHitSurfaceCFrame(HitPos,Obj)
	local SurfaceCF = {
		{"Back",Obj.CFrame * CF(0,0,Obj.Size.z)};
		{"Bottom",Obj.CFrame * CF(0,-Obj.Size.y,0)};
		{"Front",Obj.CFrame * CF(0,0,-Obj.Size.z)};
		{"Left",Obj.CFrame * CF(-Obj.Size.x,0,0)};
		{"Right",Obj.CFrame * CF(Obj.Size.x,0,0)};
		{"Top",Obj.CFrame * CF(0,Obj.Size.y,0)}
	}
	local ClosestDist = HUGE
	local ClosestSurface = nil
	for _,v in pairs(SurfaceCF) do
		local SurfaceDist = (HitPos - v[2].p).magnitude
		if SurfaceDist < ClosestDist then
			ClosestDist = SurfaceDist
			ClosestSurface = v
		end
	end
	return ClosestSurface[2]
end

function AdvRayCast(Origin, Direction, Dist, CustomIgnore)
	local NewIgnore = (CustomIgnore and CustomIgnore or Ignore)
	local NewRay = Ray.new(Origin, Direction * (Dist > 999 and 999 or Dist))
	local HitObj, HitPos = game.Workspace:FindPartOnRayWithIgnoreList(NewRay, NewIgnore)
	local LastPos = HitPos
	local FinalHitObj, FinalHitPos = nil, nil
	local RepTimes = math.floor(Dist / 999)
	if (not HitObj) and (Dist > 999) then
		for i = 0, RepTimes do
			local NewDist = (i == RepTimes and  (Dist - (LastPos - Origin).magnitude) or 999)
			local Ray2 = Ray.new(LastPos, Direction * NewDist)
			local HitObj2, HitPos2 = game.Workspace:FindPartOnRayWithIgnoreList(Ray2, NewIgnore)
			if i ~= RepTimes then
				if HitObj2 then
					FinalHitObj, FinalHitPos = HitObj2, HitPos2
					break
				end
			elseif i == RepTimes then
				FinalHitObj, FinalHitPos = HitObj2, HitPos2
			end
			LastPos = HitPos2
		end
		return FinalHitObj, FinalHitPos
	elseif HitObj or (Dist <= 999) then
		return HitObj, HitPos
	end
end
