So I already know how to change the custom walk sound
local walksound = script.Parent.Head:WaitForChild('Running') walksound.Id = 'the audio asset'
But my question is how would I change this when it is on, material or texture? I am thinking of using FindFirstChildOfClass what do you guys think?
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 --// Play sound relative to Grass. end end)
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 --// So on and so forth };
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() --// Hypothetically.
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 --// So on and so forth }; local Sounds = Character:WaitForChild("Sounds"):GetChildren() --// Hypothetically 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!