I have this footstep module that works and is very cool, but it only plays one sound per material. I want it to play random specific footstep sounds. I’ve looked everywhere but I can’t find a good tutorial anywhere.
FloorMaterial is the Enum.Material that the player is standing on. That is why it only runs once, since FloorMaterial only updates when you step on a object with a different FloorMaterial.
You could use the velocity of the player (for example) to regulate the footstep sounds
Even better connect with a function to markers in the walking animation so it matches with the walking animation.
local sounds = {"abc", "123", "asdfasdfasdf"} -- just example strings (this works with anything)
local randomSound = sounds[math.random(1,#sounds)] -- choose something random from the sounds table
print(randomSound) -- gives a random sound
math.random(1,#sounds) gets a random index from 1 (where arrays start) and #sounds, the length of the table.
When you do sounds[math.random(1,#sounds)], you get a random sound from list index 1 to list index #sounds
The other replies you got have very good answers, I’ll try to explain what they mean.
local RandomSound = sounds[math.random(1, #sounds)]
in this case, “sounds” would be an array / table of all of those individual sounds you showed an example of. math.random returns a random one of those sounds in the form of the variable “RandomSound”
Then you could play that sound in any way you like.
math.random(x, y)
x is the minimum number
y is the maximum number
in this case #sounds is the LENGTH of the sounds folder
So lets say you have a table of 6 sounds
the math.random will return a random number between 1 and 6, including 1 and 6
math.random can also be used with negative numbers, but the smaller number must come first:
local TestNumber = math.random(-6, 6)
returns a random number between (and including) -6 and 6
I suppose you could just turn this table into a library with all the sounds you want for each material placed alongside the material.
local MaterialData - {
Material1 = {
Name = "Grass",
ID = "rbxassetid://9064714296",
SoundName = --add name,
SoundID = --roblox sound id,
--any other data
},
--End Grass Data
}