Update: I tried using your method, and for some reason. It plays multiple sound tracks… I don’t understand why anyone help?
local rp = game:GetService("ReplicatedStorage")
local MusicFolder = rp:FindFirstChild("Music")
print("opa bohne")
local Musics = MusicFolder:GetChildren()
function playmusic()
local random = math.random(1,#Musics)
MusicFolder[random]:Play()
print("Track number ".. random.. " is being played.")
wait(MusicFolder[random].TimeLength)
print("Track number ".. random.. " has successfully played.")
end
for i, music in ipairs(MusicFolder:GetChildren()) do
if music:IsA("Sound") and music.IsPlaying == false then
playmusic()
end
end
I just did a repro and it works well. I used :Pause() and, after yielding, :Resume() in another script, and your code responded as expected. Using :Stop() instead of :Pause() won’t work with your original code.
When you pause the sound in another script, are you sure it affects the sound at issue? Just saying because you’d need some reference since the code you provided fires :Play() and yields .Ended event on a sound chosen randomly.
local rp = game:GetService("ReplicatedStorage")
local MusicFolder = rp:FindFirstChild("Music")
print("Loaded")
local Musics = MusicFolder:GetChildren()
function playmusic()
local playone = math.random(1,#Musics)
print(playone)
MusicFolder[playone]:Play()
MusicFolder[playone].Ended:Wait()
print("ended")
playmusic()
end
playmusic()
It worked for me when I reproduced this in studio. To emulate your sound stopping and then resuming, I used this script:
local sound = workspace.Sound
while wait(math.random(30, 50)/10) do
sound:Pause() ----<---- !!!
print(sound.TimePosition)
wait(1)
sound:Resume()
print(sound.TimePosition)
end
I adapted your code and it ran as it should, replaying the sound every time it ended, and ignoring everything else.
Note that when I paused the sound, I used :Pause(), not :Stop(). Otherwise, it will not work.
If you still have the same issue, it is most likely because you did not reference the correct object. In your code, you get a random sound from a folder. When the other script pauses a sound, it might not pause the sound you need! Please make sure you index the correct sound object there.
shouldn’t we wait for the event instead of looping? Meaning having one or multiple functions for each event depending , i.e.
– music.Ended:Connect chooseAnotherSongAndPlay()
This way, it will end by itself and send a signal Ended, instead of trying to check the state inside the function. Use a Global variable or a value instance to keep the count
Trying to do the same and I am wondering if you solved it.
So I tried something weird, normally you’d have to say :Wait(Number Here) so I decided to try :Wait(Sound.Ended), didn’t work, so I tested it some more and found wait(Sound.Ended) worked perfectly.