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:
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
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
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.
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)