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

My music script is playing multiple songs at a time. Help?

Asked by 6 years ago
Edited 6 years ago

My old music script that played Christmas songs is playing multiple songs at a time. How do I prevent / fix this?

print('ran')
local musicholder = script.Parent
math.randomseed(tick());

local songList = {
    musicholder:WaitForChild('CW'),
    musicholder:WaitForChild('DeckDubstep'),
    musicholder:WaitForChild('EhDe'),
    musicholder:WaitForChild('JayKode'),
    musicholder:WaitForChild('Ookay'),
    musicholder:WaitForChild('SantaComes'),
    musicholder:WaitForChild('XmasToMe'),
    musicholder:WaitForChild('XmasBeats'),
    --musicholder:WaitForChild('Song'),
    --musicholder:WaitForChild('Song')
    -- 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
local played = {}; --// Create a table for played songs.

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

    if played[songNum] then --// Check if the song has been played.
        repeat songNum = math.random(songCount) until not played[songNum];
        --// Choose a new song.
    end;
    played[songNum] = true; --// Record that this song has been played.
    if #played == songCount then
        played = {}; --// If all of them have been played, reset the table.
    end;

    print(songNum)
    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

1 answer

Log in to vote
0
Answered by 6 years ago

.Ended is really spooky and doesn't work the way it should on the server. You can either move the script to the client or make a table with the lengths of the sounds, like so:

local songs = {songA, songB, songC}
local songLengths = {120.5, 130, 100}

songs[songNum]:Play()
wait(songLengths[songNum])
songs[songNum]:Stop()
0
alright thanks but the issue i see with this is that the sound lengths are rounded and are very long decimals. ex. 34.41321321312312321321312312324567576465425234134143214324234234324234 Subaqueously 11 — 6y
Ad

Answer this question