Raycast Material Nil

Greetings,

I set up a very simple & generic raycast that will get the material and color of the Instance detected.

Printing the material seems to return a nil Instance value, but when the entire raycast is printed you can clearly see the material is a valid part of the ray.

Raycast
– RaycastResult{Model_Name @ 98.5538254, -0.259372652, -90.9927673; normal = 0.29143104, 0.88250792, -0.369109154; material = Neon}

Material (cap M doesn’t work either)
– ‘material’ is not a valid member of RaycastResult

Code:

local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = {part}
Params.IgnoreWater = true

local Raycast = workspace:Raycast(position, Vector3.new(0, -3, 0), Params)

print(Raycast.material)

Probably a simple fix, but any help is appreciated :slight_smile: ! WaterKnight.

Strange, when I tried to print Raycast.Material it seemed to print just fine. Lower case M definitely doesn’t work, try using a capital M again?

1 Like

Hm, you need to make sure it hits something so you can get the material at the intersection point. I would probably do…

if Raycast and Raycast.Instance then
    print(Raycast.Material or "Couldn't find material.") -- This can vary depending on the terrain data, if it hits a terrain cell.
else
    print("Didn't hit anything.")
end

Well, in fact, it must hit something so the raycasting results can return given properties instead of nil. That might be what is happening although I’d check just to make sure.

4 Likes

@OKevinO , @Avanthyst ,

Thanks for the help! A combination of both these strategies seem to work flawlessly.

1 Like