Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 7 years ago
Edited 7 years ago

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

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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
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. karolus1 8 — 7y
0
You didn't ask for them to be random in the question c: OldPalHappy 1477 — 7y
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 ;) karolus1 8 — 7y
0
D: OldPalHappy 1477 — 7y
0
You're right, sorry. OldPalHappy 1477 — 7y
Ad

Answer this question