I can't manage to get a random Id from a table in a module script

Hi everyone and Happy New Year! I hope you guys can help me on this one.

So I’m trying to make a system that produces a footstep sound every step I take based on the material you are on, but I’m having some issues with the SoundId. I got multiple sounds Id for each materials stored in a module script, this is so the footsteps are not repetitive and annoying like the default Roblox walking sound.

I’m gonna take for example the material “Fabric”, here is how I stored it in my module script :

local footsteps = {}
footsteps.sounds = {
	["Fabric"] = {
		"rbxassetid://9126748130",
		"rbxassetid://9126747861",
		"rbxassetid://9126747720",
		"rbxassetid://9126747529",
	},
}

footsteps.materialMap = {
	[Enum.Material.Fabric] = 		"Fabric",
}

return footsteps

I got the idea to put a animation event called “Footstep” every time one of the feet makes contact with the ground in each of my animations. So here is my script for the running animation :

local footsteps = require(script:WaitForChild("Footsteps"))

anim["frontrun"]:GetMarkerReachedSignal("Footstep"):Connect(function()
	if humanoid.WalkSpeed > 20 then
		local material = footsteps.materialMap[humanoid.FloorMaterial]
		local soundId = footsteps.sounds[math.random(1, #material)]
		local sound = Instance.new("Sound")
		sound.Name = "FootstepSound"
		sound.Parent = root
		sound.SoundId = soundId
		print(sound.SoundId)
	end
end)

My only problem is that I receive nil for the sound Id.

Here is my error :

image

I am grateful to anyone that helps me!

what i see:

local material=string
local soundid=footstep.sounds[math.random(1,#length_of_string)] --6
--(length_of_string is 6 or so, there are 4 elements in footsteps.sounds["Fabric"]

what probably should happen (at least for it to not error):

local soundid=footsteps.sounds[material] 
sound.SoundId=soundid[math.random(#soundid)]

(this will still most likely error if a humanoid steps on a material that isnt in the material table, you can fix this by actually checking that)

1 Like

Thank you it worked! And also because I don’t have a sound for every material well I had already assigned those to a material similar that has a sound id :

footsteps.materialMap = {
	[Enum.Material.Fabric] = 		"Fabric",
	
	[Enum.Material.Slate] = 		"Concrete",
	[Enum.Material.Concrete] = 		"Concrete",
	[Enum.Material.Brick] = 		"Concrete",
	[Enum.Material.Cobblestone] = 	"Concrete",
	[Enum.Material.Sandstone] =		"Concrete",
	[Enum.Material.Rock] = 			"Concrete",
	[Enum.Material.Basalt] = 		"Concrete",
	[Enum.Material.CrackedLava] = 	"Concrete",
	[Enum.Material.Asphalt] = 		"Concrete",
	[Enum.Material.Limestone] = 	"Concrete",
	[Enum.Material.Pavement] = 		"Concrete",
	[Enum.Material.Pebble] = 		"Concrete",
	
	[Enum.Material.Ground] = 		"Dirt",
	[Enum.Material.Mud] = 			"Dirt",

	[Enum.Material.Grass] = 		"Grass",
	[Enum.Material.LeafyGrass] = 	"Grass",
	
	[Enum.Material.Sand] = 			"Sand",
	[Enum.Material.Salt] = 			"Sand",
	
	[Enum.Material.Snow] = 			"Snow",
	[Enum.Material.Glacier] = 		"Snow",
	
	[Enum.Material.Plastic] = 		"Tile",
	[Enum.Material.Marble] = 		"Tile",
	[Enum.Material.Granite] = 		"Tile",
	[Enum.Material.SmoothPlastic] = "Tile",

	[Enum.Material.Wood] = 			"Wood",
	[Enum.Material.WoodPlanks] = 	"Wood",
	
	[Enum.Material.CorrodedMetal] = "Metal",
	[Enum.Material.DiamondPlate] = 	"Metal",
	[Enum.Material.Metal] = 		"Metal",
	[Enum.Material.Foil] = 			"Metal",

	[Enum.Material.Ice] = 			"Glass",
	[Enum.Material.Glacier] = 		"Glass",
	[Enum.Material.Glass] = 		"Glass",
	[Enum.Material.Neon] = 			"Glass",
	[Enum.Material.ForceField] = 	"Glass",
}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.