Attempt to get length of a userdata value but they are objects not a userData Value

im trying to make my own sound system but this error i could not fix

attempt to get length of a userdata value
14:19:38.318 - Stack Begin
14:19:38.321 - Script 'ServerScriptService.SoundCaller', Line 9
14:19:38.322 - Stack End

Here is the code:

local Module = require(game.ReplicatedStorage:WaitForChild("SoundPlayer"))

while wait(0.5) do
	local Sounds = game.ServerStorage.Sounds:GetDescendants()
	
	
	for _, Sond in pairs(Sounds) do
		
		local sound = math.random(1, #Sond)
		print(sound.Name) -- prints the name of the sound
		sound:Play() -- plays the sound
	end
	-- [[ if randomSong:IsA("Sound") then
		--local ClonedSong = randomSong:Clone()
		--ClonedSong.Parent = workspace.Sound
		--Module:PlaySong(ClonedSong)
		
		--wait(randomSong.TimeLength)
		
		--Module:DestroySong(ClonedSong)
	-- ]] end
end

You are using a in pairs loop and you put Sond as a variable for each object in the Sounds. Instead of local sound = math.random(1, #Sond)which is only 1 value do: local sound = math.random(1, #Sounds)

but it will print their index which is not helpful

Try this one out:

local sound = math.random(1, #sounds)

for i,v in pairs(Sounds) do
    if i == sound then
       v:Play()
    end
end
1 Like