How do I detect if the player is walking on a custom material?

I have a footstep sound script that uses FloorMaterial to check which material is the player stepping on.

However I use material variants for other materials like acid or water.
Yet FloorMaterial only shows the base material, not it’s variant

So how could I achieve this?

2 Likes

If you want to detect the specific variant of a material (for example, different types of water or acid) in Roblox, you can use :GetMaterialColor() , which returns the color value of a material. The color value can be used to identify different variants of a material.

2 Likes

For an example

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local customMaterialStorage = ReplicatedStorage:WaitForChild("CustomMaterials") -- Make a folder name it what ever and put all your materials in there
local footstepSound = script.Parent

local function onTouched(hit)
    local character = hit.Parent
    local humanoid = character:FindFirstChildOfClass("Humanoid")

    if humanoid then
        local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
        if humanoidRootPart then
            local ray = Ray.new(humanoidRootPart.Position, Vector3.new(0, -2, 0)) -- Raycast down from the player's position
            local hitPart, hitPosition, hitNormal = workspace:FindPartOnRay(ray, character)

            if hitPart then
                local material = hitPart.Material
                local customMaterialVariant = customMaterialStorage:FindFirstChild(material.Name)

                if customMaterialVariant then
                    -- Handle custom material variant logic here
                    print("Player stepped on a custom material variant:", customMaterialVariant.Name)
                    footstepSound:Play()
                end
            end
        end
    end
end

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
        humanoidRootPart.Touched:Connect(onTouched)
    end)
end)
1 Like

Humanoid.FloorMaterial is the simplest
way

1 Like

What’s the difference between “Ray.new” and “workspace:Raycast()”?

ray.new is the deprecated version of workspace:raycast, you should use workspace:raycast

1 Like

Ohhhh okay!!!
I thought it was gonna be another cool function to learn about :frowning:

Thank you!

Ray.new() is the constructor for the Ray UserData type, which just bundles together two Vector3s, an origin point and a directional vector. It’s not a raycast at all, and it was only used by the old raycasting functions from years ago, like FindPartOnRay(), etc. There’s no longer any reason to use it in new code.

The easiest way is to just read Part.MaterialVariant off the part(s) the avatar is standing on.I get the part references by raycasting down from each foot, which allows for each foot to make the correct sound in the cases where they are on different materials.

This also works, thank you!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.