function FindFirstClass(O,D)local g=nil for O,Y in pairs(O:GetChildren())do if Y.ClassName==D then g=Y break end end return g end function IsIgnored(O)for D,g in pairs(Ignore)do if O==g or O:IsDescendantOf(g)then return true end end return false end function GetHitSurfaceCFrame(O,D)local g={{"\066\097\099\107",D.CFrame*CF(0,0,D.Size.z)},{"\066\111\116\116\111\109";D.CFrame*CF(0,-D.Size.y,0)};{"\070\114\111\110\116";D.CFrame*CF(0,0,-D.Size.z)};{"\076\101\102\116",D.CFrame*CF(-D.Size.x,0,0)};{"\082\105\103\104\116";D.CFrame*CF(D.Size.x,0,0)},{"\084\111\112",D.CFrame*CF(0,D.Size.y,0)}}local Y=HUGE local B=nil for D,g in pairs(g)do local f=(O-g[2].p).magnitude if f<Y then Y=f B=g end end return B[2]end function AdvRayCast(O,D,g,Y)local B=Y and Y or Ignore local f=Ray.new(O,D*(g>999 and 999 or g))local x,o=game.Workspace:FindPartOnRayWithIgnoreList(f,B)local S=o local w,J=nil,nil local y=math.floor(g/999)if not x and g>999 then for Y=0,y,1 do local f=Y==y and g-(S-O).magnitude or 999 local x=Ray.new(S,D*f)local o,i=game.Workspace:FindPartOnRayWithIgnoreList(x,B)if Y~=y then if o then w,J=o,i break end elseif Y==y then w,J=o,i end S=i end return w,J elseif x or g<=999 then return x,o end 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)
	for _,v in pairs(Ignore) 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
