Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make a sound play when the player is on a certain type of material?

Asked by 5 years ago

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?

1 answer

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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
      --// Play sound relative to Grass.
   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
   --// 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!

0
Thanks! I didn't really understand tables. I guess it is like the case function in javascript Dev_Coda 31 — 5y
0
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. Ziffixture 6913 — 5y
Ad

Answer this question