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

Why won't both of my random music scripts work?

Asked by 7 years ago
Edited 7 years ago

I'm not sure how to do it, but I tried both methods. Can someone help me find what's wrong? By the way, the script is inside a ServerStorage model named MusicHolder

SCRIPT NUMBER ONE

local musicholder = script.Parent
local music = musicholder:GetChildren()
local time1 = game.ServerStorage.MusicHolder.CW.TimeLength
local time2 = musicholder.DeckDubstep.TimeLength
local time3 = musicholder.EhDe.TimeLength
local time4 = musicholder.JayKode.TimeLength
local time5 = musicholder.Ookay.TimeLength
local time6 = musicholder.SantaComes.TimeLength
local time7 = musicholder.XmasToMe.TimeLength
local randomsong = math.random(1, #music)

while true do
    wait(1)
    musicholder:WaitForChild("CW","DeckDubstep", "EhDe", "JayKode", "Ookay", "SantaComes", "XmasToMe")
    wait(7)
    if randomsong == 1 then
        musicholder.CW:Play()
        wait(time1)
    end
    if randomsong == 2 then
        musicholder.DeckDubstep:Play()
        wait(time2)
    end
    if randomsong == 3 then
        musicholder.EhDe:Play()
        wait(time3)
    end
    if randomsong == 4 then
        musicholder.JayKode:Play()
        wait(time4)
    end
    if randomsong == 5 then
        musicholder.Ookay:Play()
        wait(time5)
    end
    if randomsong == 6 then
        musicholder.SantaComes:Play()
        wait(time6)
    end
    if randomsong == 7 then
        musicholder.XmasToMe:Play()
        wait(time7)
    end
end

SCRIPT NUMBER TWO

local musicholder = script.Parent
local music = musicholder:GetChildren()
local randomsong = math.random(1, #music)
local cw  = musicholder.CW
local dd = musicholder.DeckDubstep
local ehde = musicholder.EhDe
local jk = musicholder.JayKode
local okay = musicholder.Ookay
local sc = musicholder.SantaComes
local xmas = musicholder.XmasToMe

while true do
    wait(7)
    if randomsong == 1 then
        cw:Play()
        if cw.Stopped == true then
        end
    end
    if randomsong == 2 then
        dd:Play()
        if dd.Stopped == true then
        end
    end
    if randomsong == 3 then
        ehde:Play()
        if ehde.Stopped == true then
        end
    end
    if randomsong == 4 then
        jk:Play()
        if jk.Stopped == true then
        end
    end
    if randomsong == 5 then
        okay:Play()
        if okay.Stopped == true then
        end
    end
    if randomsong == 6 then
        sc:Play()
        if sc.Stopped == true then
        end
    end
    if randomsong == 7 then
        xmas:Play()
        if xmas.Stopped == true then
        end
    end
end
0
Could you please tell us what is exactly happening? personal_ly 151 — 7y
0
um im attempting to choose a random song with math.random but songs dont even play at all :| i play the song and wait until the end then choose another song randomly Inpolite 44 — 7y

2 answers

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

In both scripts you create the random number and store this in a variable, you then reuse this same variable giving you the same song each time. In addition to this WaitForChild only accepts one string. e.g.

musicholder:WaitForChild("CW","DeckDubstep", "EhDe", "JayKode", "Ookay", "SantaComes", "XmasToMe")

Should be

musicholder:WaitForChild("CW")
-- other items

You can also use elseif as only need one of them to run then play the next song.

We can also simplify a lot of this script by using the Ended event for the sound with Wait() so we only wait till the ended event is fired then choose the next song.

Putting it all together:-

local musicholder = script.Parent

local songList = {
    musicholder:WaitForChild('XmasToMe') -- we wait for each song
    -- add the rest of the songs to this list
}

local songCount = #songList -- this returns the number of items in the table above so we only change as little code as possible when adding songs

while true do
    local songNum = math.random(1, songCount) -- gets a random number in the range of the number of songs 

    songList[songNum]:Play() -- we access the sound in the table and play it
    songList[songNum].Ended:Wait() -- we wait until the song has finished then play anther song
end

I hope this helps please comment if you do not understand how / why this script works.

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

The scripts look fine, the only thing that might be left is to the contentprovider to wait for the audios to load.

-- // Preloading //

local contprovider = game:GetService("ContentProvider")

local ids = { } -- // Ids of each audio

for _, audioids in ipairs(ids) do -- Loads the audios inside the table.
    contprovider:Preload("http://www.roblox.com/asset/?id=" .. audioids)
end

while contprovider.RequestQueueSize > 0 do -- Just a wait for them to complete loading.
    wait(0.1)
end

Answer this question