Having a few problems with my radio module

Here’s the sitch fellas.

I have a radio module that goes through a folder of songs in ReplicatedStorage and starts playing them in a sound called “Sync”. Sync cannot be heard, it’s volume is at 0, but radios around my game can tune into the Sync for a particular channel. My problem is this:

  1. When the song switches, it does switch in Sync but it does not switch on the radio. It merely repeats the same song after it’s over. The song does change if you turn the radio off and on again though. This only happens for two of my channels though, the first one and the second one. My debug channel is working just fine, but all the sound files in my debug channel are only a few seconds long.

  2. When you turn the radio off and on again after the song switches once, it restarts the song from the beginning. This happens to all my channels. It’s supposed to take the TimePosition of the Sync and start the song on that, but it does not do this.

Here is the code and the hierarchy.
eyJhbGciOiJIUzI1NiJ9.eyJpbWciOiJfYmYxNzA3Y2NhY2FlNWYyN2JkODM4MTkzMTk2NTgzZmMifQ.kFqwxz1ZdFHiMq3G2dv23lJEZk6KyLdnA0pP-sDfJfg-png

Serverside script that changes the Syncs:

--< RADIO
local radio = EC.Radio

local function radioStationThread(station)
	station.Sync.Playing = true
	while true do
		for _, song in ipairs(station.Songs:GetChildren()) do
			if song:FindFirstChild("PitchCorrection") ~= nil then station.Sync.PlaybackSpeed = song.PitchCorrection.Value else station.Sync.PlaybackSpeed = 1 end
			station.Sync.SoundId = song.Value
			station.Sync.TimePosition = 0
			if station.Name ~= "3" then print("Station", station.Name, "is now playing", song.Name) end
			wait(station.Sync.TimeLength)
		end
	end
end

for _, x in ipairs(radio:GetChildren()) do
	spawn(function() radioStationThread(x) end)
end
-->

Module script utilized when radio is interacted with:

--radio

module.totalStations = tonumber(table.getn(game.ReplicatedStorage.EC.Radio:GetChildren()))

function module.updateRadioText(model, station)
	model.CN.N.Title.Text = station.StationName.Value
	model.CF.F.Freq.Text = station.StationFreq.Value
end

function module.tune(model, station)
	local speaker = model.RadioSelect.Speaker
	local sync = station.Sync
	speaker.SoundId = sync.SoundId
	speaker.TimePosition = sync.TimePosition
	speaker.Playing = true
	wait(1)
	speaker.Ended:Connect(function() module.tune(model, game.ReplicatedStorage.EC.Radio:FindFirstChild(model.RadioSelect.Station.Value)) end)
end

function module.radioToggle(model)
	model.RadioSelect.Switch:Play()
	local speaker = model.RadioSelect.Speaker
	if speaker.Playing then 
		model.CN.N.Title.Text = ""; model.CF.F.Freq.Text = ""
		speaker.Playing = false 
		return 
	end
	local station = game.ReplicatedStorage.EC.Radio:FindFirstChild(tostring(model.RadioSelect.Station.Value))
	module.updateRadioText(model, station)
	module.tune(model, station)
end

function module.freqChange(model, pom)
	if not model.RadioSelect.Speaker.Playing then return end
	local stn = model.RadioSelect.Station
	local n = 1
	if pom == false then n = -1 end
	stn.Value = stn.Value + n
	if stn.Value > module.totalStations then stn.Value = 1 elseif stn.Value < 1 then stn.Value = module.totalStations end
	
	local stnFol = game.ReplicatedStorage.EC.Radio:FindFirstChild(stn.Value)
	module.updateRadioText(model, stnFol)
	module.tune(model, stnFol)
end

function module.freqChangeI(model)
	module.freqChange(model, true)
end

function module.freqChangeD(model)
	module.freqChange(model, false)
end

return module

I’ve been at this for hours and getting nowhere, help would be appreciated

It’s a bit hard to understand the problems with only these scripts. Could you provide a place file that we can play with?

Yeah sure thing, trouble is though the first song is about 3 minutes so you’d have to wait 3 minutes for it to finish to notice any issues. Here’s the link:

Have you tried using data store for the second problem? Documentation - Roblox Creator Hub

How would that work? Like store the timeposition?

Yes, store the timeposition. You also might want to add debounce

You can substitute this with station.Sync.Ended:Wait(). Also be careful with recursive functions, as they seems to be able to cause memory leaks, if you can think about how many scopes it expands into.

Ensure that you have not checked Looped property to true.

Remember to check all the API in Sound:
https://developer.roblox.com/en-us/api-reference/class/Sound

I can’t exactly store the TimePosition. What the radio is supposed to do is tune into an existing sound that’s already playing, setting it’s own time position to that of the Sync.

Yes, but the function is calling itself every few minutes. This is a long debounce. Am I still vulnerable to memory leaks after that amount of time has passed?

Could you make the place open source for a while?

How would one do that

30charlimit

Supposedly you can utilize a while loop and a boolean that changes from the toggle, instead of a recursive function. The loop will stop entirely if you have the radio toggled off or some other statements.

wouldn’t I only be able to break the loop after an entire song has ended?

Side note sorry if I seem a bit ignorant, I’m a bit of a novice at this

Configure this place > Permissions > Allow Copying

Image 1

Image 2

If the sound was stopped and the yielding stops, it will then move onto the while check and if the while condition is not true, it will stop the loop. This loop will keep the radio playing and changing its songs over time.

Also are you sure that the sync is playing?

Alright, I’ve done it

30charlimit

Yes, I am sure that the sync is playing.

I put in the station.Sync.Ended:Wait() but it now seems to be repeating the same song over and over, even in the debug station.

Here is the output if you can see anything

  07:50:45.512  Station 2 is now playing GymnopedieNo1  -  Server  -  State:26
  07:50:45.625  Station 1 is now playing ItsBeenALongLongTime  -  Server  -  State:26
  07:50:52.425  Station 2 is now playing KaiserWaltzerOp437  -  Server  -  State:26
  07:50:52.778  Station 1 is now playing MyWay  -  Server  -  State:26
  07:50:52.835  Station 2 is now playing WeddingMarchMendelssohn  -  Server  -  State:26
  07:50:53.321  Station 1 is now playing ICantHelpMyself  -  Server  -  State:26
  07:50:53.386  Station 2 is now playing StraussFantasia1  -  Server  -  State:26
  07:50:53.387  Station 1 is now playing InTheShadowOfTheValley  -  Server  -  State:26
  07:50:53.426  Station 2 is now playing RadetzkyMarch  -  Server  -  State:26
  07:50:53.426  Station 1 is now playing LaughingOnTheOutside  -  Server  -  State:26```