#Floor Material
Luckily, FloorMaterial is a a property pertaining to the Humanoid, predominantly to give you the Material the Character is standing on.
For your Scripts intentions, you'd be using Conditionals for each specific Material Enum.
For this example, we'll use Grass as the ideal Material.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.Running:Connect(function()
if (Humanoid.FloorMaterial == Enum.Material.Grass) then
end
end)
#Advanced additions
Though this will work, it wouldn't be too great to have a cluster of if statements bombarding your Listener, to identify each specific outcome. Instead, we can clean up the Code to play whichever sound relative to the Material beneath their feet, through a Universal Conditional. (Dictionary Comparisons)
Firstly, we'd have to create the Dictionary with the desired, possible outcomes.
local RelativeMaterials = {
["Grass"] = Enum.Material.Grass;
["Sand"] = Enum.Material.Sand
};
With this Dictionary, we can use a special loop designed to iterate through each Element. Passing back the Name, you'll see why that's important later, and the Material Enum.
Secondly, we'd have to reference the Sounds we want to play, make sure they're Named accordingly to each Dictionary Name, (Grass, Sand, etcetera) and are in a Folder called Sounds.
local Sounds = Character:WaitForChild("Sounds"):GetChildren()
Now with these, we apply them to our current Script, and use the said loop to create our Universal Conditional.
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local RelativeMaterials = {
["Grass"] = Enum.Material.Grass;
["Sand"] = Enum.Material.Sand
};
local Sounds = Character:WaitForChild("Sounds"):GetChildren()
Humanoid.Running:Connect(function()
for Name, material in ipairs(RelativeMaterials) do
if (Humanoid.FloorMaterial == Material) then
Sounds[Name]:Play()
end
end
end)
Hope this helps! don't forget to accept this answer and optionally upvote too:) Have a nice day!
2 comments
Thanks! I didn't really understand tables. I guess it is like the case function in javascript
It's not entirely, I see how would would grasp it, though I wouldn't keep that in mind. See this link for an understanding: http://wiki.roblox.com/index.php/Tables.