Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

More efficient way to play music?

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

✓ Accepted answer

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.

4 comments

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?

0

i might be able to answer seperatley

0

newstring will be the property that changed, so this won't work

0

no, newtring will be the value. Learn what your saying before you attempt to correct others who actually know what they are doing.

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.

3 comments

0

To make it play a random song, and have easy access to that, could I do

0

local random_sound = math.random[songs]

0

Yeah you can. Never thought about it that way lmao

This discussion is preserved as it was. New answers, comments, and votes are closed.