Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Music Player Script That Plays Random Music Isn't Working, Help?

local songsIds =  {445943699,  201680547,       910813585,      154870011,     226294028}
while true do wait()
x = math.random(#1,songsIds)
y = songsIds[x]
local Song = Instance.new("Sound",game.Workspace)
Song.SoundId = "http://roblox.com/asset?id="..y
Song.Volume = 10
Song.PlaybakcSpeed = 1
Song.Looped = false
Song.MaxDistance = 99999
Song:Play()
debris:AddItem(Song,Song.TimeLength)
wait(Song.TimeLength - 3)
for fade = 1,10,0.1 do wait(0.1)
	Song.Volume = Song.Volume - 0.01
end end

It throws the error...

-- Workspace.SoundPlayer.Script:11: attempt to get length of a number value

I've tried changing the table to just a pile of strings like

local songsIds = "445943699","201680547","910813585","154870011","226294028"

I tried

x = (tostring(math.random(#1,songsIds)))

and I've tried

x = (tonumber(math.random(#1,songsIds)))

Throws the same exact error each time.

2 answers

I added an iteration in the songsIds table But the simple solution to your script was that on line 3 in your script you did local x = math.random(#1,songsIds) but you don't add # to an Int or number value its supposed to be local x = math.random(1,#songsIds) and btw I also reccommend while wait() do end instead of while true do wait() end

	local songsIds =  {445943699,  201680547,  910813585,  154870011, 226294028}
while wait() do -- I reccommend while wait() do instead of while true do wait() , minor diffrence
local Song = Instance.new("Sound",workspace) -- Wanted to keep it short
for songsAmount,soundObject in pairs(songsIds) do
	local x = math.random(1,soundObject)
	local y = songsIds[x]
	Song.SoundId = "http://roblox.com/asset?id="..y
	Song.Volume = 10
	Song.PlaybakcSpeed = 1
	Song.Looped = false
	Song.MaxDistance = 99999
	Song:Play()
	debris:AddItem(Song,Song.TimeLength)
	wait(Song.TimeLength - 3)
for fade = 1,10,0.1 do wait(0.1)
    	Song.Volume = Song.Volume - 0.01
end
end
end

Hopefully this fixes your error Please submit this answer if it worked! :)

1 comment

0

The code you added doesn't work - specifically, the songsIds for loop.

You didn't use the hash correctly, here's the fix:

x = math.random(1,#songsId)

You put the hash in the wrong place.


Hope I helped!

~TDP

This discussion is preserved as it was. New answers, comments, and votes are closed.