Need help with Raycasting error

Hello! I am modding the classic Roblox Jeep to use Worldroot:Raycast() instead of Worldroot:FindPartOnRayWithIgnoreList(). However, when I drive on a Ramp, or Terrain, I get this error:
image

I modded the RaycastingModule to this:

-- RaycastModule.lua, written by LocalWE

local module = {}

function module.new(startPosition, startDirection)
	local maxDistance = startDirection.magnitude
	local direction = startDirection.unit
	local lastPosition = startPosition
	local distance = 0
	local ignore = {}
	
	local hit
	
	repeat
		local ray = Ray.new(lastPosition, direction * (maxDistance - distance))
		local raycastParams = RaycastParams.new(ignore, Enum.RaycastFilterType.Blacklist, true, "Default")
		hit = workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
		--hit, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
		if hit then
			if not hit.Instance.CanCollide then
				table.insert(ignore, hit)
			end
		end
		distance = (startPosition - hit.Position).magnitude
		lastPosition = hit.Position
	until distance >= maxDistance - 0.1 or (hit.Instance and hit.Instance.CanCollide)
	return hit.Instance, hit.Position, hit.Normal
end

return module

If you can help, please let me know! Thanks, WE

Code that uses ‘hit’ is outside of the if statement that checks if hit exists in the first place. It is erroring because hit does not exist.

It can get to the return statement if the distance is greater than or equal to the max distance. It’s possible that on the last raycast hit is nil, so you end up indexing nil. It could also be this line:
lastPosition = hit.Position

Wait so I am confused. Doesn’t hit exist? It would not error with the old raycasting system.

Line 12:

local hit

Line 17-19

hit = workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
		--hit, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
		if hit then

So what would I change to fix it? hit is a RaycastResult? Isn’t it?

UPDATE!

Forgot to add this:
LocalCarScript:

local hit, position = Raycast.new(thruster.Position, thruster.CFrame:VectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)

It has the same error.

I think you misunderstand. The error says that you indexed nil with position, which means that hit doesn’t exist. Check if hit exists before indexing it.

1 Like

Ok, managed to fix that, now in line 28, this errors:

until distance >= maxDistance - 0.1 or (hit.Instance and hit.Instance.CanCollide)

image
The line 20:

local hit, position = Raycast.new(thruster.Position, thruster.CFrame:VectorToWorldSpace(Vector3.new(0, -1, 0)) * stats.Height.Value)

And Idk how to do the if it exists or not in an until.

I think you can do

until distance >= maxDistance - 0.1 or (hit and hit.Instance and hit.Instance.CanCollide)

To check if hit exists first before checking if the instance of hit exists

That kind of lagged my computer, and generated this error: image

Oh you don’t have a wait in your loop, try this

repeat
		local ray = Ray.new(lastPosition, direction * (maxDistance - distance))
		local raycastParams = RaycastParams.new(ignore, Enum.RaycastFilterType.Blacklist, true, "Default")
		hit = workspace:Raycast(ray.Origin, ray.Direction, raycastParams)
		--hit, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore, false, true)
		if hit then
			if not hit.Instance.CanCollide then
				table.insert(ignore, hit)
			end
		end
		distance = (startPosition - hit.Position).magnitude
		lastPosition = hit.Position
        game:GetService("RunService").Heartbeat:Wait()
	until distance >= maxDistance - 0.1 or (hit and hit.Instance and hit.Instance.CanCollide)
1 Like

That just made the jeep do this:


gonna try wait()

Edit: It does the same thing.