So im working on a club and in the club i have a music box and the music box plays different songs. I scripted one, it worked but then stopped and started to clash into songs and stuff, Like an idiot I am I got rid of the script and I need it for help, can you guys help me?
How to script a music box that plays different songs?
2 answers
Normally you should always try to have music played on the client. I think we can do this. Let's try.
Playing the sounds in sequence
To do this, let's first get a Local Script.
-- Local Script
Then we should get the music. I would suggest either A.) Creating a table of the IDs, or B.) Create a Folder in the script, "Or somewhere else on the client", and play the music in workspace.
-- Local Script In StarterPack
local musicFolder = script:WaitForChild("MusicFolder"):GetChilren()
:GetChildren returns a table of children on what it's called on
No we can loop through the sounds, Clone them with :Clone(), set the sound's parent to workspace, and play it. We should also define the boom-box, and parent the sound to the boom-box so it plays at the Boom-box's location.
-- Local Script In StarterPack
local musicFolder = script:WaitForChild("MusicFolder"):GetChilren()
local boombox = workspace:WaitForChild("BoomBox")-- Define the music box
for i,v in pairs(musicFolder) do
local sound = v:Clone()
sound.Parent = boombox
sound:Play()
sound.Ended:wait()
end
Line 9 waits for the sound to end
Now to make this run forever, we should throw this in a while loop. We should also delete the sound when it finishes to clean things up.
-- Local Script In StarterPack
local musicFolder = script:WaitForChild("MusicFolder"):GetChilren()
local boombox = workspace:WaitForChild("BoomBox")-- Define the music box
while true do
for i,v in pairs(musicFolder) do
local sound = v:Clone()
sound.Parent = boombox
sound:Play()
sound.Ended:wait()
sound:Destroy()
end
end
Playing a random sound
To make the sequence random, all we have to do is pick a random index in our table using math.random(). We can find out the max index using #table.
-- Local Script In StarterPack
local musicFolder = script:WaitForChild("MusicFolder"):GetChilren()
local boombox = workspace:WaitForChild("BoomBox")-- Define the music box
while true do
local sound = musicFolder[math.random(#musicTable)]
sound.Parent = boombox
sound:Play()
sound.Ended:wait()
sound:Destroy()
end
Playing a random sound
Warning
This is probably in essence a good solution. However, this will only work if FE is off, and you don't mind having people hear different sounds/songs. To fix this, you can play the sounds on the server. To do this, we "should" be able to do the same as above in a regular script in the part, but waiting for the length of the sound.
-- Regular Script in part
local musicFolder = script:WaitForChild("MusicFolder"):GetChilren()
local boombox = script.Parent
while wait() do
local sound = musicFolder[math.random(#musicTable)]
sound.Parent = boombox
sound:Play()
wait()--Safety
wait(sound.TimeLength)
sound:Destroy()
end
Plays server-sided
if I helped, please don't forget to accept my answer
We need the script..
1 comment
I said I deleted it..
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
Thanks so much! And I didnt forget to accept the answer
I added more in case you use studio with FE off, which I believe you do.