Trying to make random music play

i know there’s several topics on this but i have a folder of songs that i cant really get to play. it picks a random number from how many songs are in the folder but its throwing me an error like this:
image

ty!

script:

local songs = game:GetService("SoundService").songs

while wait() do
	for i, song in pairs(songs:GetChildren()) do
		if song:IsA("Sound") then
			local randomNo = math.random(1, #songs:GetChildren())
local randomSong = song[randomNo]
randomSong:Play()
randomSong.Ended:Wait()
		end
	end
end
5 Likes

At the bottom, there is 3 end.
Did you see any red lines?

3 Likes


no errors?

2 Likes

replace line 7 with local randomSong = songs:GetChildren()[randomNo]

3 Likes

I noticed something.
You should remove :GetChildren() next to songs at line 6.

2 Likes

It doesn’t like line 6.

local randomNo = math.random(1, #songs:GetChildren())

So, is songs a valid item?

I is most likely the way you have identified songs in line 1.

Try WaitForChild

2 Likes

absolutely perfect mate, amazing job. thank u so much 4 ur help :smiley:

1 Like

Now I can’t get marked as solution. :smiling_face_with_tear:

2 Likes

u were right. im not very good at indexing but the solution was to do with putting [randomNo] after. thanks so much for ur help tho! :smiley:

so sorry mm, u almost had it but thanks a million for ur help i really appreciate it. :smiley:

It’s fine.
Maybe next time. Because I don’t want to go into an argument.

1 Like

solution for those who are stuck: (credits: every1 who replied :smiley: )

local songs = game:GetService("SoundService").songs

while wait() do
	for i, song in pairs(songs:GetChildren()) do
		if song:IsA("Sound") then
			local randomNo = math.random(1, #songs:GetChildren())
			local randomSong = songs:GetChildren()[randomNo]
			randomSong:Play()
			randomSong.Ended:Wait()
		end
	end
end

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