I made a system where a radio would randomly select and play songs and switch to a different one after one has finished. However, it is rapidly switching songs even though the song hasn’t finished yet.
Here’s my code:
wait(1)
while true do
local Sounds = script:GetChildren()
local queue = script.Parent.queue
if queue:FindFirstChildOfClass('Sound') then
local sound = queue:FindFirstChildOfClass('Sound'):Clone()
sound.Parent = script.Parent.playing
script.Parent.Parent.Radio.antenna.BillboardGui.TextLabel.Text = "Now Playing: "..sound:FindFirstChild('name').Value
sound:Play()
wait(sound.TimeLength)
sound:Destroy()
else
local RandomIndex = math.random(1,#Sounds)
local RandomSound = Sounds[RandomIndex]:Clone()
RandomSound.Parent = script.Parent.playing
script.Parent.Parent.Radio.antenna.BillboardGui.TextLabel.Text = "Now Playing: "..RandomSound.Name
RandomSound:Play()
wait(RandomSound.TimeLength)
RandomSound:Destroy()
end
end