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
.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()