Script that plays a footstep sound on animation event, but only when touching the ground?

I’m trying to make a script that plays a random footstep sound (from a list of 5 that I’ve prepared) when an animation event is triggered, but only if the player doing the animation is touching Terrain (under Workspace). I specifically want the sound to come from the player that’s doing the animation, and make it so everyone can hear each other’s footsteps.

I already have the animations with the events prepared, and verified that they are being triggered. Problem is I don’t know how to script the actual sounds, and asking the AI assistant to write the scripts for me didn’t help much. The scripts it made didn’t work, even after I spent half an hour trying to fix it by describing the errors to the AI.

Can someone please help me with this?

I believe you can use :Play() to play a soundtrack, :Pause() to pause it, and :Stop() to stop it.

1 Like

Yes, I know that, but how do I write the rest of the script? Check for the animation event, check if the player is touching Terrain, randomize the sound, make the sound global, make it come from the player character…?

You could use Humanoid.FloorMaterial to check what they character is standing on.

You can place the sound in the HumanoidRootPart so the sound RollOff works properly.

You can play the Sound with a Server script so everyone within the RollOff distance hears the sound.

you could put this script along with the folder containing the sounds in the character you want the sound to play from. Don’t put the script in the folder or it might cause an error.

local hum = (player character):WaitForChild("Humanoid")
local walking = false
local soundList = (soundFolder):GetChildren()

hum.StateChanged:Connect(function(old, new)
	if new == Enum.HumanoidStateType.Running then
		walking = true
	else
		walking = false
	end
end)

while walking do
	if hum.FloorMaterial ~= Enum.Material.Air then
		local randomSound = soundList[math.random(1, #soundList)]
		randomSound:Play()
		task.wait(randomSound.TimeLength)
	end
    task.wait()
end

if the sounds don’t stop playing with the above script then wrap the while loop in a coroutine.

coroutine.resume(coroutine.create(function()
    --loop here
end))