Detecting the material of terrain with a touched function?

script.Parent.Touched:Connect(function(hit)
	if hit:IsA("Terrain") then
		print(hit.Material)
	end
end)

I was checking to see if I can print the material of the terrain using a touch event. but instead of printing the terrain material it prints “Enum.Material.Plastic”( which is weird because the closest source of plastic is the part itself).

7 Likes

Depending on your needs, you could either raycast or check Humanoid.FloorMaterial

2 Likes

If you’re trying to find what material a player is standing on, you should use something like this:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild"Humanoid"

humanoid:GetPropertyChangedSignal"FloorMaterial":Connect(function()
    print(humanoid.FloorMaterial)
end)
9 Likes

In addition to FloorMaterial, you can downcast, which is probably what the Humanoid does to get FloorMaterial anyway (unless they use Touched, which would be gross). Downcasting would be just creating a ray between the root and a direction of around 3 studs below the root.

Ray.new(RootPart.Position, Vector3.new(0, -3, 0))

Run that every frame and check the material via FindPartOnRay (the fourth argument specifically).

3 Likes

i just need to check if its touching a specific material.

the only two things ill be using for the filtering is:
.Touched
FindPartsInRegion3()

I have a pre created region already and all I need to do is determine if the terrain thats inside is sand or grass, etc.

There is no method on Terrain-like instances for checking what material it is mainly composed of, according to the dev wiki.

  • .Touched only returns part instances, reading the Terrain's material will always return the Plastic enum.

  • FindPartsInRegion3 returns an array of part instances with no specific material, and has the same problem as .Touched.

The only way to find the material of a specific part of terrain is by drawing a ray into the region. The material returned should be the material found on that portion of the terrain. The only other alternative method I can think of is storing the material along with the region in a dictionary-like structure. I’m afraid there is nothing else you can use than this.

9 Likes

ok, well thanks for your help anyway.

1 Like