Error: invalid argument #2 to 'random' (interval is empty)

I have this footstep sound script, and I keep getting this error, what is happening?

-- Variables --

local playerService = game:GetService("Players")
local userInputService = game:GetService("UserInputService")
local localPlayer = playerService.LocalPlayer
local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = localPlayer:GetMouse()
local footStepStorage = game.ReplicatedStorage:WaitForChild("FootstepLibrary")
local val = script:WaitForChild("StepValue")
local running = false

-- Settings --

local walkSpeed = 10
local runSpeed = 24

-- Animations --	

-- Walk --
local animWalk = Instance.new("Animation")
animWalk.AnimationId = "rbxassetid://8899736432"
local walkAnim = humanoid:LoadAnimation(animWalk)

-- Run --
local animRun = Instance.new("Animation")
animRun.AnimationId = "rbxassetid://8899661986"
local runAnim = humanoid:LoadAnimation(animRun)

-- Idle --e
local animIdle = Instance.new("Animation")
animIdle.AnimationId = "rbxassetid://8899759590"
local idleAnim = humanoid:LoadAnimation(animIdle)

-- Main Functions --
	
function Footstep()
	val.Value = humanoid.FloorMaterial.Name
	local soundFolder = footStepStorage:FindFirstChild(val.Value)
	local step = soundFolder[math.random(1, #soundFolder:GetChildren())]
--	print("Step on "..soundFolder.Name)
	game.ReplicatedStorage.StepEvent:FireServer(character.Head,step)
--	local s = step:Clone()
--	s.Parent = character.Head
--	s:Destroy()
end

humanoid.Running:Connect(function(speed)
	if speed > 0.05 and humanoid.WalkSpeed < runSpeed and not walkAnim.IsPlaying and not running then
		idleAnim:Stop(0.5)
		walkAnim:Play(0.5)
		walkAnim:AdjustSpeed(humanoid.WalkSpeed/walkSpeed)
		runAnim:Stop(0.5)
	elseif speed < 0.05 then
		walkAnim:Stop(0.5)
		idleAnim:Play(0.5)
		runAnim:Stop(0.5)
	elseif speed > 0.05 and humanoid.WalkSpeed >= runSpeed and not runAnim.IsPlaying then
		idleAnim:Stop(0.5)
		walkAnim:Stop(0.5)
		runAnim:Play(0.5)
		runAnim:AdjustSpeed(humanoid.WalkSpeed / runSpeed)
	end
end)

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = runSpeed
			running = true
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed = walkSpeed
			running = false
	end
end)


walkAnim:GetMarkerReachedSignal("Footstep"):Connect(function()
	if walkAnim.IsPlaying then
		Footstep()
	end
end)

runAnim:GetMarkerReachedSignal("Footstep"):Connect(function()
	if runAnim.IsPlaying then
		Footstep()
	end
end)

Can you confirm there’s sounds located in soundFolder?

Yes, as shown here.
image

Try this code snippet instead:

local footstepSounds = footStepStorage:GetChildren()
local step = footstepSounds[math.random(1, #footstepSounds)]

I think you were accessing the wrong folder perhaps.

I’m not sure, but I’m pretty sure the script gets the material of the ground from the boolvalue present in the script.
image
image

Did you want there to be a random sound to be played for each material? Or is there only one sound for each material?

If it’s just one sound, you don’t need to grab a random sound.

Just play the sound you already have reference to:

function Footstep()
	val.Value = humanoid.FloorMaterial.Name
	local stepSound = footStepStorage:FindFirstChild(val.Value)
        
      if stepSound then
          stepSound:Play()
     end
end

Ah, so that’s what it was.

What I had were the sounds themselves, and what I think this script was supposed to do was get the sounds from a folder sharing the name with the material you are standing on. Thanks anyway.

1 Like