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

More efficient way to play music?

Asked by 5 years ago

I made a script to play music in the background of my game, and when a round starts, change the sound. But, it runs off of the status string value (the status value is what controls the bar at the top that shows players "Intermission", etc.) I feel like there is a more efficient way to do this. Also, how could I make a sort of playlist that plays a random song instead of just the one? Heres my script:

while true do

    game.Workspace.Wind:Stop()
    game.Workspace.BGM:Play()

    repeat wait (1) until game.ReplicatedStorage.Status.Value == "Start!"

    game.Workspace.BGM:Stop()
    game.Workspace.Wind:Play()

    repeat wait (1) until game.ReplicatedStorage.Status.Value == "Intermission" or game.ReplicatedStorage.Status.Value == "Waiting For More Players..."

end

2 answers

Log in to vote
0
Answered by
mc3334 649 Moderation Voter
5 years ago

There is! Instead of useing a while loop function, you can use .Changed on the string value. Ill provide a quick snippet of code on how this could be used.

stringValue.Changed:Connect(function(newstring)
    if newstring == "Start!" then
        --Code when string = start here
    elseif newstring == "Intermission" then
        --Code when string = intermission here
    elseif newstring == "Waiting For More Players..." then
        --Code when string = waiting for players here
    end
end)    

With any further questions, feel free to reply to my answer.

1
Thank you for that! Also, could you answer the other part of my question: How could I make a sort of looping playlist, and every time that a song ends it plays a random song from that playlist? NickIsANuke 217 — 5y
0
i might be able to answer seperatley KDarren12 705 — 5y
0
newstring will be the property that changed, so this won't work User#23252 26 — 5y
0
no, newtring will be the value. Learn what your saying before you attempt to correct others who actually know what they are doing. mc3334 649 — 5y
Ad
Log in to vote
0
Answered by
KDarren12 705 Donator Moderation Voter
5 years ago

To answer the second portion of your question, you can use a table to list all of the songs you have. I'm using a placeholder, "SONGIDHERE". You need to change them yourself.

songs = {SONGIDHERE,SONGIDHERE,SONGIDHERE} -- You could add more and more if you'd like.
for i = 1,#songs do
for i,v in pairs(game:GetDescendants()) do
if v:IsA("Sound") then
v:Stop()
end
end
local song = Instance.new("Sound")
song.Name = "song"
song.SoundId = songs[i]

You can add a changed option to any other sound in the game. I've never searched through the descendant of game before, and I'm not sure if it's possible. If not, please comment on this post.

0
To make it play a random song, and have easy access to that, could I do NickIsANuke 217 — 5y
0
local random_sound = math.random[songs] NickIsANuke 217 — 5y
0
Yeah you can. Never thought about it that way lmao KDarren12 705 — 5y

Answer this question