Help with loop overloaded and not able to do anything - Raycast Edition

So I get this error:
image
And I tried adding

game:GetService("RunService").Heartbeat:Wait()

But:


I guess the kart is really… Out of this world xD

Anyways, all jokes aside, here is the script.
I am trying to convert WorldRoot:FindPartOnRayWithIgnoreList() with WorldRoot:Raycast(). My guess is that Deprecated Raycasting functions have a delay or something.:

local module = {}

function module.new(startPosition, startDirection)
	local maxDistance = startDirection.magnitude
	local direction = startDirection.unit
	local lastPosition = startPosition
	local distance = 0
	local ignore = {}
	
	local Result

	repeat
		game:GetService("RunService").Heartbeat:Wait()
		local ray = Ray.new(lastPosition, direction * (maxDistance - distance))
		local DefaultParams = RaycastParams.new()
		DefaultParams.IgnoreWater = true
		DefaultParams.FilterType = Enum.RaycastFilterType.Blacklist
		DefaultParams.FilterDescendantsInstances = ignore
		Result = workspace:Raycast(ray.Origin, ray.Direction, DefaultParams)

		if Result then
			if not Result.Instance.CanCollide then
				table.insert(ignore, Result.Instance)
			end

			distance = (startPosition - Result.Position).magnitude
			lastPosition = Result.Position
		end
	until distance >= maxDistance - 0.1 or (Result and Result.Instance.CanCollide)
	return Result.Instance, Result.Position, Result.Normal
end

return module

Please help, thanks, WE

You will need to update the distance if there is no hit to avoid the infinite loop and add a reentry limit like the original source of the code.

		if hit then
			collide = (hit.CanCollide and hit.Transparency < 0.9)
			if not collide then
				table.insert(ignore, hit)
			end
		end
--notice the distance updates and last position as well
		distance = (startPosition - position).magnitude
		lastPosition = position

Also you need to take into account the limitation of raycast params to which you cannot use table .insert by itself.

Considering this limitation it might be best to just stick with the old raycasting method. Not sure why you need to update it unless you want to use collision groups as the old method of raycasting works as is.

Personally I just use this option as described here:

  1. Instead of using the CanCollide property, make a new Collision Group which doesn’t collide with Default and set all of the CanCollide false parts’ Collision Group to it.

Collision groups are better than managing that ignore list table.

More attempts at loop casting with the newer api:

1 Like