How do I stop a sound from repeating in a Footstep Sound script?

Got this from Sqaku, worked great but it’s supposed to play random sounds, the problem is that it repeatedly plays the same sound multiple times before switching

--[[
  Sqaku's footstep sounds v1.1
  
  --//What's new?\\--
  
  --Configuration
  --Some bug fixes
  
  --\\-----------//--

  
  PLACE IN STARTER CHARACTER SCRIPTS!!!
  _____  ___    ____  __  _  __ __ 
 / ___/ /   \  /    ||  |/ ]|  |  |
(   \_ |     ||  o  ||  ' / |  |  |
 \__  ||  Q  ||     ||    \ |  |  |
 /  \ ||     ||  _  ||     ||  :  |
 \    ||     ||  |  ||  .  ||     |
  \___| \__,_||__|__||__|\_| \__,_|
                                   
  Disco: sqaku #3003 (DM me if you find bug-s!)
  Roblox Profile: Squakmire
  
  Thanks for using the script!
]]

local manager = require(script:WaitForChild("Manager"))
local configs = require(script:WaitForChild("Configuration"))
local soundsFolder = manager:CreateSoundsFolder(game:GetService("SoundService"))

local humanoid = script.Parent:WaitForChild("Humanoid")

function createSound(soundId)
	local sound = Instance.new("Sound")
	sound.Volume = configs.Volume
	sound.SoundId = soundId
	sound.Parent = game:GetService("SoundService")
	
	coroutine.wrap(function()
		sound:Play()
		task.wait(sound.TimeLength)
		sound:Destroy()
	end)()
end

while true do
	if humanoid.MoveDirection.Magnitude > 0 then
		local material = string.lower(tostring(humanoid.FloorMaterial))
		local bits = string.split(material, ".")
		material = bits[3]
		local id, vol = manager:GetRandomSound({tostring(material), soundsFolder})
		createSound(id)
		task.wait(configs.Factor / humanoid.WalkSpeed)
	else
		task.wait()
	end
end

Manager Script

local manager = {}

local SoundsList = {
	["plastic"] = { --ADD MORE TO THE TABLE AND IT'LL RANDOMIZE / SWITCH BETWEEN RANDOM SOUNDS LISTED IN THE TABLE
		"rbxassetid://7326203155",
		"rbxassetid://9126730713",
		"rbxassetid://9126730782",
		"rbxassetid://9126731037",
		"rbxassetid://9126730980",
		"rbxassetid://9126730651",
		"rbxassetid://9126730563",
		"rbxassetid://9126730279",
		"rbxassetid://9126730403",
		"rbxassetid://9126730056",
		"rbxassetid://9126730172",
		"rbxassetid://9126729836",
		"rbxassetid://9126730472",
		"rbxassetid://9126729938",
		"rbxassetid://9126729706"
	};
	
	["grass"] = {
		"rbxassetid://6501217328",
		"rbxassetid://9126742396",
		"rbxassetid://9126741427",
		"rbxassetid://9126742333",
		"rbxassetid://9126742215",
		"rbxassetid://9126742271",
		"rbxassetid://9126742031",
		"rbxassetid://9126741934",
		"rbxassetid://9126742105",
		"rbxassetid://9126741826",
		"rbxassetid://9126741594",
		"rbxassetid://9126741512",
		"rbxassetid://9126741741",
		"rbxassetid://9126741674"
	};
	
	["metal"] = {
		"rbxassetid://6876957898",
		"rbxassetid://9126739090",
		"rbxassetid://9126738967",
		"rbxassetid://9126738896",
		"rbxassetid://9126738732",
		"rbxassetid://9126738543",
		"rbxassetid://9126738634"
	};
	
	["wood"] = {
		"rbxassetid://2812419402",
		"rbxassetid://9126931624",
		"rbxassetid://9126931515",
		"rbxassetid://9126931417",
		"rbxassetid://9126931322",
		"rbxassetid://9126931699",
		"rbxassetid://9126931235",
		"rbxassetid://9126931169",
		"rbxassetid://9126931026",
		"rbxassetid://9126930953",
		"rbxassetid://9126930885",
		"rbxassetid://9126930789",
		"rbxassetid://9126930647",
		"rbxassetid://9126930516",
		"rbxassetid://9126930598",
		"rbxassetid://9126930718"
	};
	
	["concrete"] = {
		"rbxassetid://5761648082",
		"rbxassetid://9126746167",
		"rbxassetid://9126746098",
		"rbxassetid://9126745995",
		"rbxassetid://9126745877",
		"rbxassetid://9126745774",
		"rbxassetid://9126745574",
		"rbxassetid://9126745336",
		"rbxassetid://9126745241",
		"rbxassetid://9126745445",
		"rbxassetid://9126745052",
		"rbxassetid://9126745141",
		"rbxassetid://9126745676",
		"rbxassetid://9126744969",
		"rbxassetid://9126744894",
		"rbxassetid://9126744639",
		"rbxassetid://9126744789",
		"rbxassetid://9126744481"
	};
	
	["fabric"] = {
		"rbxassetid://6240702531",
		"rbxassetid://9126748130",
		"rbxassetid://9126747861",
		"rbxassetid://9126747720",
		"rbxassetid://9126747529",
		"rbxassetid://9126747412",
		"rbxassetid://9126747283",
		"rbxassetid://9126746732",
		"rbxassetid://9126746837",
		"rbxassetid://9126747132",
		"rbxassetid://9126746984",
		"rbxassetid://9126746598",
		"rbxassetid://9126746481",
		"rbxassetid://9126746371",
		"rbxassetid://9126746291"
	};
}

local MaterialMap = {
	smoothplastic = SoundsList.plastic;
	forcefield =    SoundsList.plastic;
	foil =          SoundsList.plastic;
	
	woodplanks =    SoundsList.wood;
	
	diamondplate =  SoundsList.metal;
	corrodedmetal = SoundsList.metal;
	
	sand =          SoundsList.grass;
	leafygrass =    SoundsList.grass;
	ground =        SoundsList.grass;
	mud =           SoundsList.grass;
	snow =          SoundsList.grass;
	glacier =       SoundsList.grass;
	
	pavament =      SoundsList.concrete;
	limestone =     SoundsList.concrete;
	salt =          SoundsList.concrete;
	asphalt =       SoundsList.concrete;
	glass =         SoundsList.concrete;
	neon =          SoundsList.concrete;
	crackedlava =   SoundsList.concrete;
	basalt =        SoundsList.concrete;
	sandstone =     SoundsList.concrete;
	rock =          SoundsList.concrete;
	cobblestone =   SoundsList.concrete;
	pebble =        SoundsList.concrete;
	brick =         SoundsList.concrete;
	granite =       SoundsList.concrete;
	marble =        SoundsList.concrete;
	ice =           SoundsList.concrete;
	slate =         SoundsList.concrete;
}

function manager:CreateSoundsFolder(parent)
	local folder = Instance.new("Folder")
	folder.Name = "FootstepSounds"
	folder.Parent = parent
	
	for name , material in pairs(SoundsList) do
		local materialFolder = Instance.new("Folder")
		materialFolder.Name = name
		materialFolder.Parent = folder
		
		for j , id in ipairs(material) do
			local sound = Instance.new("Sound")
			sound.Name = "variant_" .. j
			sound.SoundId = id
			sound.Volume = 2
			sound.Parent = materialFolder
		end
	end
	
	for name, material in pairs(MaterialMap) do
		local materialFolder = Instance.new("Folder")
		materialFolder.Name = tostring(name)
		materialFolder.Parent = folder

		for j , id in ipairs(material) do
			local sound = Instance.new("Sound")
			sound.Name = "variant_" .. j
			sound.SoundId = id
			sound.Volume = 2
			sound.Parent = materialFolder
		end 
	end
	
	return folder
end

function manager:GetRandomSound(j_)
	self.title = j_[1]
	self.soundsFolder = j_[2]
	math.randomseed(tick())
	
	local sound = SoundsList[self.title] or MaterialMap[self.title]
	if sound then
		self.soundsFolder = self.soundsFolder[self.title]
		local footstep = self.soundsFolder:GetChildren()[math.random(1, #self.soundsFolder:GetChildren())]
		return footstep.SoundId, footstep.Volume
	else
		return ""
	end
end

return manager

Do you mean like this Video?

Yeah, it’s basically the exact same script, except I’ve added a few sounds more, the problem is that one sound repeats multiple times

function manager:GetRandomSound(j_)
	self.title = j_[1]
	self.soundsFolder = j_[2]
	
	local sound = SoundsList[self.title] or MaterialMap[self.title]
	if sound then
		self.soundsFolder = self.soundsFolder[self.title]
		local footstepSounds = self.soundsFolder:GetChildren()
		local footstepIndex = math.random(1, #footstepSounds)
		local footstep = footstepSounds[footstepIndex]
		return footstep.SoundId, footstep.Volume
	else
		return ""
	end
end

return manager

This version of the script removes the call to math.randomseed and instead generates a random index into the footstepSounds array using the math.random function. This should ensure that a different sound is played each time the function is called, as long as the footstepSounds array contains multiple sounds.

don’t judge me btw I used openai chatgpt 3 to fix your script

Click this video:

I wouldn’t recommend this as only you will be able to hear this because it’s all coming from 1 local script. obviously if your game/experience is single player, this will not matter.

1 Like