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

Why it won't select randomly sound values from the list?

Hey, i want to make simple play/stop music gui, but it play only one music from the list. I don't know what should i use here? I thought for loop is all what i needed, but i guess it need something more.

Script:

local sound = script.Parent.Parent.PlayableSound
local musicList = script.Parent.MusicList

for _, music in pairs(musicList:GetChildren()) do
	sound.SoundId = "rbxassetid://".. music.Value
	sound:Play()
end

Gui screenshot: https://gyazo.com/37911080a1bfe0fc192005acc1483104

SoundID is NumberValue. Maybe i need to rename SoundID to 1, 2, 3 etc. ?

1 answer

✓ Accepted answer

Basically it loops so fast through all the sounds, and you don't hear any of them until the loop ends and it plays the last song.

Just wait inside the loop for the sound to end. You can only do this in a Local Script.

--// Local Script:

local sound = script.Parent.Parent.PlayableSound
local musicList = script.Parent.MusicList

for _, music in pairs(musicList:GetChildren()) do
	sound.SoundId = "rbxassetid://".. music.Value
	sound:Play()
	sound.Ended:Wait() -- Wait for the sound to end.
end

Edit:

As for making it random, you need to use math.random().

You can index a table using a number to get a direct value within the table. Using math.random, and getting the size of the table using #table, we can get a random music.

--// Local Script:

local sound = script.Parent.Parent.PlayableSound
local musicList = script.Parent.MusicList:GetChildren()

while true do
	local music = musicList[math.random(#musicList)] -- Random Music
	sound.SoundId = "rbxassetid://".. music.Value
	sound:Play()
	sound.Ended:Wait() -- Wait for the sound to end.
end

5 comments

0

Oh.. okay. I know it's local script ;) Anyway i wonder it play them randomly? becuase i think it just start from the begin and keep going.

0

You didn't ask for them to be random in the question c:

0

Well, i asked "Why it won't select randomly sound values from the list?" :P But thank you, i'll figure it out by myself ;)

0

D:

0

You're right, sorry.

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