function Raycast.FindPartOnRayWithCallbackWithIgnoreList(J,q,Y,d,Q,G)local P=J.Direction.Magnitude local g=J.Direction.Unit Q=Q or 25 local t=0 local X=J.Origin local L=0 local N,s,v,f repeat local C=Ray.new(X,g*(P-L))N,s,v,f=WORKSPACE:FindPartOnRayWithIgnoreList(J,q,Y,d,true)local e=G(N,s,v,f)if e==CALLBACKRESULT.Continue then L=(J.Origin-s).Magnitude X=s elseif e==CALLBACKRESULT.Finished then return N,s,v,f elseif e==CALLBACKRESULT.Fail or e==nil then break end t=t+1 until t>Q or L>=P-.1 return end function Raycast.FindPartOnRayWithCallbackWithWhiteList(J,q,Y,d,Q,G)local P=J.Direction.Magnitude local g=J.Direction.Unit Q=Q or 25 local t=0 local X=J.Origin local L=0 local N,s,v,f repeat local C=Ray.new(X,g*(P-L))N,s,v,f=WORKSPACE:FindPartOnRayWithWhitelist(J,q,Y,d,true)if N then end local e=G(N,s,v,f)if e==CALLBACKRESULT.Continue then L=(J.Origin-s).Magnitude X=s elseif e==CALLBACKRESULT.Finished then return N,s,v,f elseif e==CALLBACKRESULT.Fail or e==nil then break end t=t+1 until t>Q or L>=P-.1 return end function Raycast.FindPartOnRayWithCallback(J,q,Y,d,Q)return Raycast.FindPartOnRayWithCallbackWithIgnoreList(J,{q},Y,d,Q)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
