function Raycast.FindPartOnRayWithCallbackWithIgnoreList(X,N,K,V,f,q)local s=X.Direction.Magnitude local A=X.Direction.Unit f=f or 25 local T=0 local Y=X.Origin local G=0 local D,o,B,z repeat local y=Ray.new(Y,A*(s-G))D,o,B,z=WORKSPACE:FindPartOnRayWithIgnoreList(X,N,K,V,true)local b=q(D,o,B,z)if b==CALLBACKRESULT.Continue then G=(X.Origin-o).Magnitude Y=o elseif b==CALLBACKRESULT.Finished then return D,o,B,z elseif b==CALLBACKRESULT.Fail or b==nil then break end T=T+1 until T>f or G>=s-.1 return end function Raycast.FindPartOnRayWithCallbackWithWhiteList(X,N,K,V,f,q)local s=X.Direction.Magnitude local A=X.Direction.Unit f=f or 25 local T=0 local Y=X.Origin local G=0 local D,o,B,z repeat local y=Ray.new(Y,A*(s-G))D,o,B,z=WORKSPACE:FindPartOnRayWithWhitelist(X,N,K,V,true)if D then end local b=q(D,o,B,z)if b==CALLBACKRESULT.Continue then G=(X.Origin-o).Magnitude Y=o elseif b==CALLBACKRESULT.Finished then return D,o,B,z elseif b==CALLBACKRESULT.Fail or b==nil then break end T=T+1 until T>f or G>=s-.1 return end function Raycast.FindPartOnRayWithCallback(X,N,K,V,f)return Raycast.FindPartOnRayWithCallbackWithIgnoreList(X,{N},K,V,f)end
--

function Raycast.FindPartOnRayWithCallbackWithIgnoreList(
	ray,
	list,
	terrainCellsAreCubes,
	ignoreWater,
	loopout,
	callbackFunc
)
	local maxDistance = ray.Direction.Magnitude
	local direction = ray.Direction.Unit

	loopout = loopout or 25
	local loopCount = 0

	local lastPosition = ray.Origin
	local distance = 0

	local hit, position, normal, material

	repeat
		local r = Ray.new(lastPosition, direction * (maxDistance - distance))
		hit, position, normal, material =
			WORKSPACE:FindPartOnRayWithIgnoreList(ray, list, terrainCellsAreCubes, ignoreWater, true)
		local result = callbackFunc(hit, position, normal, material)

		if result == CALLBACKRESULT.Continue then
			distance = (ray.Origin - position).Magnitude
			lastPosition = position
		elseif result == CALLBACKRESULT.Finished then
			return hit, position, normal, material
		elseif result == CALLBACKRESULT.Fail or result == nil then
			break
		end

		loopCount = loopCount + 1
	until loopCount > loopout or distance >= maxDistance - 0.1

	return
end

function Raycast.FindPartOnRayWithCallbackWithWhiteList(
	ray,
	list,
	terrainCellsAreCubes,
	ignoreWater,
	loopout,
	callbackFunc
)
	local maxDistance = ray.Direction.Magnitude
	local direction = ray.Direction.Unit

	loopout = loopout or 25
	local loopCount = 0

	local lastPosition = ray.Origin
	local distance = 0

	local hit, position, normal, material

	repeat
		local r = Ray.new(lastPosition, direction * (maxDistance - distance))
		hit, position, normal, material =
			WORKSPACE:FindPartOnRayWithWhitelist(ray, list, terrainCellsAreCubes, ignoreWater, true)
		if hit then
			--print(hit:GetFullName())
		end
		local result = callbackFunc(hit, position, normal, material)

		if result == CALLBACKRESULT.Continue then
			distance = (ray.Origin - position).Magnitude
			lastPosition = position
		elseif result == CALLBACKRESULT.Finished then
			return hit, position, normal, material
		elseif result == CALLBACKRESULT.Fail or result == nil then
			break
		end
		loopCount = loopCount + 1
	until loopCount > loopout or distance >= maxDistance - 0.1

	return
end

function Raycast.FindPartOnRayWithCallback(ray, ignore, terrainCellsAreCubes, ignoreWater, callbackFunc)
	return Raycast.FindPartOnRayWithCallbackWithIgnoreList(
		ray,
		{ ignore },
		terrainCellsAreCubes,
		ignoreWater,
		callbackFunc
	)
end
