I’m new to coding please help.
It plays the first song and then stops unless it’s looped.
local id = {"5410085694","5410086218","5410081542"}
local chosenid = id[math.random(1, #id)]
while true do
wait(1)
local m = Instance.new("Sound",Workspace)
m.Looped = true
m.Name = "GamePlayAudio1"
m.SoundId = "rbxassetid://"..chosenid
m.Volume = 2
m:Play()
wait(5)
if m.Playing == true then
repeat
wait()
until m.Playing == false
end
return
end
Hey there, one thing I noticed right off the bat is that you have “Workspace” capitalized when defining your new instance. You should correct it to either:
This is why the next song never plays. Also, I noticed this:
You’re selecting the song only once since it’s outside the while loop. Here’s some improved code(not tested):
local Songs = { "5410085694","5410086218","5410081542" }
local random = Random.new()
while task.wait(1) do
local ChosenSong = Songs[random:NextInteger(1,#Songs)]
local Sound = Instance.new("Sound")
Sound.Parent = workspace
Sound.Name = "GamePlayAudio1"
Sound.SoundId = "rbxassetid://"..ChosenSong
Sound.Volume = 2
Sound:Play()
Sound.Ended:Wait()
end
Tried it again and it seemed to work, but thats Roblox for you I guess, but there’s a new audio every time a new song is picked. Is there a way to delete the old audio? Or use the same one? Audio as in the speaker.
local Songs = { "5410085694","5410086218","5410081542" }
local random = Random.new()
local Sound = [PATH_TO_SOUND]
while task.wait(1) do
local ChosenSong = Songs[random:NextInteger(1,#Songs)]
Sound.SoundId = ChosenSong
Sound:Play()
Sound.Ended:Wait();Sound.SoundId = ""
end
local Randoms = Random.new()
local Sound = Instance.new("Sound")
Sound.Name = "GamePlayAudio1"
Sound.Volume = 2
Sound.Parent = workspace
local Sounds = { "5410085694","5410086218","5410081542" }
while true do
local RandomSound = Sounds[Randoms:NextInteger(1, #Sounds)]
Sound.SoundId = "rbxassetid://"..RandomSound
if not Sound.IsLoaded then
Sound.Loaded:Wait()
end
Sound:Play()
Sound.Ended:Wait()
end
It would be more efficient to just instance a single “Sound” instance and use that instead. You should probably wait for the “Sound” instance to load each time its “SoundId” property is changed before calling the instance method “:Play()” on it.