Hi, i been trying to solve this and i dont really know how to solve it so thats why i made this post, what im trying to do is a sound system that respect anothers sounds (e.g: a win sound play randomly but to stop another sound playing at the same time it stops for the sound already playing to play another sound)
this is the code i’ve tried to create, i also tried with a function but i ended with the “while true do” bcs it didn’t let me use “repeat”
local CarpetaSonidos = game.Workspace.Sonidos
local Aullidos = CarpetaSonidos.Aullidos
local Grillos = CarpetaSonidos.Grillos
local Buho1 = CarpetaSonidos.Buho1
local Buho2 = CarpetaSonidos.Buho2
while true do
print("Numero")
local Numero = math.random(2,4)
wait(math.random(1,5))
print("Start")
if Numero == 1 then
Aullidos:Play()
elseif Numero == 2 then
Grillos:Play()
elseif Numero == 3 then
Buho1:Play()
elseif Numero == 4 then
Buho2:Play()
end
print("AudioPlayed")
Aullidos.Ended:Wait()
Grillos.Ended:Wait()
Buho1.Ended:Wait()
Buho2.Ended:Wait()
print("Repeat")
end
local CarpetaSonidos = workspace:WaitForChild("Sonidos")
local Aullidos = CarpetaSonidos:WaitForChild("Aullidos")
local Grillos = CarpetaSonidos:WaitForChild("Grillos")
local Buho1 = CarpetaSonidos:WaitForChild("Buho1")
local Buho2 = CarpetaSonidos:WaitForChild("Buho2")
local SoundList = {Aullidos, Grillos, Buho1, Buho2}
local CurrentSound = SoundList[math.random(1, #SoundList)]
while task.wait(math.random(1, 5)) do
local RandomSound = SoundList[math.random(1, #SoundList)]
CurrentSound:Stop()
CurrentSound = RandomSound
CurrentSound:Play()
end
“SoundList” is used to group all the sounds into a table, making it easier to select a random sound without the need for a series of if statements. This line SoundList[math.random(1, #SoundList)] allows for the direct selection of a random sound from the first to the last in the table.
“CurrentSound” serves as a reference to the sound that is currently playing, allowing you to stop and change it without having to stop all the other sounds, as you might not know which one is playing.