I’m creating a system where you can mute songs (parented in the script), the problem is that touching the block that mute the songs would give a timeout in some wait() lines, which i marked in the script:
local IsPlayerInAllowedArea = true
local DefaultVolume = 0.5
local DelayToStartNextSong = 0.5
function StopSongs()
if IsPlayerInAllowedArea then
IsPlayerInAllowedArea = false
local Songs = script:GetChildren()
for _, Song in pairs(Songs) do
if Song.IsPlaying then
Song:Stop()
end
end
end
end
function PlaySongs()
if not IsPlayerInAllowedArea then
IsPlayerInAllowedArea = true
print("Play")
end
end
function GetRandomSong(PreviousSong)
local Songs = script:GetChildren()
local RandomSong
repeat
RandomSong = Songs[math.random(#Songs)]
until
RandomSong ~= PreviousSong
return RandomSong
end
local CurrentSong
coroutine.resume(coroutine.create(function()
while true do
if IsPlayerInAllowedArea then
wait(0.5)
local SelectedSong = GetRandomSong(CurrentSong)
CurrentSong = SelectedSong
print(CurrentSong)
CurrentSong:Play()
CurrentSong.Stopped:Wait() --//Timeout here//--
end
end
end))
end)
local StopSongsBrick = game.Workspace:FindFirstChild("StopSongs")
local e = false
StopSongsBrick.Touched:Connect(function(Obj)
if Obj.Parent.Name == game.Players.LocalPlayer.Name and not e then
e = true
StopSongs()
wait(0.5)--//Timeout here//--
e = false
end
end)
Explanation about how the script works
IsPlayerInAllowedArea would determine if the songs are going to play, StopSongs() would stop every song that is playing. PlaySongs() would set IsPlayerInAllowedArea true, which would make the next loop select a random song and play it. There’s a block touched function, which would fire the StopSongs() function.
I don’t know how timeouts are occurring, i really need help in this script, if you need more info, feel free to ask me. Thanks for reading.