Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to ignore "Air" material when using :FindPartOnRay()?

Hi I am trying to use FindPartOnRay() to detect terrain type of "Mud" but I keep getting "Air."

mouse.Move:Connect(function()
	local ray,point,surfaceNormal,material = game.Workspace:FindPartOnRay(Ray.new(game.Workspace.Camera.CFrame.Position,mouse.Hit.Position)
	print(material)
end)
--Always prints air, even when pointed at a plastic brick or Mud terrain

1 answer

mouse.Move:Connect(function()
    local ray,point,surfaceNormal,material = game.Workspace:FindPartOnRayWithIgnoreList(mouse.UnitRay,{Enum.Material.Air})
    print(material)
end)

The "FindPartOnRayWithIgnoreList" is what you were looking for.

Also just found the possible reason why Enum.Material.Air does not work for an ignore list, it is set to not browsable https://wiki.roblox.com/index.php?title=API:Class_reference/ReflectionMetadata

I came up with another idea, but it didn't work, I'll put the code here anyway (it does work for other applications)

I made a region3 from the camera's position to the mouse position and used Terrain:ReadVoxels() to get the terrain along the path of the ray, but the problem in this case is that Enum.Material.Air is not able to be detected by ReadVoxels

mouse.TargetFilter = workspace.Part

function ignoreList(first,last)
    local region = Region3.new(last, first)
	print(region.CFrame," SIZE ",region.Size)
	workspace.Part.CFrame=region.CFrame--this places a part in the middle of the region so you can visualize where the region is
    region = region:ExpandToGrid(4)
    local material,occupancy = game.Workspace.Terrain:ReadVoxels(region, 4)
    local size = material.Size
	local allMats = {}
    for x = 1, size.X do
    	for y = 1, size.Y do
    		for z = 1, size.Z do
    			allMats[#allMats+1]=material[x][y][z]
				
    		end
    	end
    end
	return allMats
end

5 comments

0

That yields an "Unable to cast value to Object" error

0

I know, I'm currently trying to figure out why it's doing that, but I'm assuming it has something to do with using the mouse.UnitRay

0

the main problem is that "Air" is not an object. It has no parent so we don't know how to reference it

0

oh I see

0

But this is still a creative way to get the material of the terrain, even if we can't get past the air

This discussion is preserved as it was. New answers, comments, and votes are closed.