"Unable to cast value to Object" when attempting to raycast

Here is my code.

local CheckRay = Ray.new(Ball.Position, Vector3.new(0,Ball.Size.Y / 2 + .05,0 ))

local WhiteList = {workspace.Terrain, workspace.Water:GetChildren()}

local Hit = workspace:FindPartOnRayWithWhitelist(CheckRay,WhiteList,true )

while wait(0.1) do

print(Hit)

end

What am I doing wrong?

This will return a data structure that looks like this:

{workspace.Terrain, { ... the children of workspace.Water ... }}

Note the nested table, it’s not a flat table of just objects, it has an inner table. You get the error because that inner table is not an object.

You can create the whitelist like this instead:

local WhiteList = workspace.Water:GetChildren()
table.insert(WhiteList, workspace.Terrain)

This way, it stays a flat table without inner tables, since you are just adding a value to the end of the table (workspace.Terrain, in this case).


Note also that your while-loop is pointless, it will print the same value every 0.1 seconds. If you meant to keep doing raycasts, then move the raycast call into the while-loop instead.

1 Like

I threw the raycast call into the loop, but it just prints nil no matter what.

That means the raycast has no hit. If you were to move an object that is in the whitelist over the ray, it would start printing a hit.

The part I am casting from is right on top of the brick it should be hitting.

The brick is under the folder “Water” hence GetChildren()

I have tried lengthening the ray, but it doesnt make a difference.

You’re not casting down, you’re casting up.

This is what you’re doing:

local CheckRay = Ray.new(Ball.Position, Vector3.new(0,Ball.Size.Y / 2 + .05,0 ))

This means your ray starts at the Ball.Position, and the direction is the second value. That would be a positive number since size.Y is always positive. So you are casting upwards.

It is now constantly printing “Terrain” as the hit, but also while its in the air. Is it counting air as terrain?

(I have changed the direction of the ray)

WAIT, the ray isnt moving with the ball!