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.
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)
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.