Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Global radio not functioning?

For some reason, I made this radio to globally play songs and loop, but it plays the first song then ends. I have absolutely no clue why its doing this. (I've already tried replacing Ended with Stopped, it came to no avail.) It gives no error in the console. I can't find out any working way to make it switch.

local id1 = "rbxassetid://215717985"
local id2 = "rbxassetid://193424165"
local id3 = "rbxassetid://263562509"
local id4 = "rbxassetid://466347974"
local id5 = "rbxassetid://512934146"

mori = game.Workspace.MORIOHRADIO
game.Workspace.MORIOHRADIO.SoundId = id1
mori:Play()
mori.Ended:Connect(function()
	if game.Workspace.MORIOHRADIO.SoundId == id1 then
		mori.SoundId = id2
		mori:Play()
	end
	if game.Workspace.MORIOHRADIO.SoundId == id2 then
		mori.SoundId = id3
		mori:Play()
	end
	if mori.SoundId  == id3 then
		mori.SoundId = id4
		mori:Play()
	end
	if mori.SoundId == id4 then
		mori.SoundId = id5
		mori:Play()
	end
	if mori.SoundId == id5 then
		mori.SoundId = id1
		mori:Play()
	end
end)

1 comment

0

I Replicated your script, But in a Formal way Maybe this might help you? https://gyazo.com/3a2e32a6578ef93b812c3d85e10a3b80

1 answer

✓ Accepted answer

You're assigning SoundId to the next sound, then the next if statements are executed, and since you changed the SoundId, the conditions were met.

All 5 if statements are executing and playing the sound!

If you used elseif, you would fix the problem, as the other bodies don't get executed if one of the conditions is already truthy:

local id1 = "rbxassetid://215717985"
local id2 = "rbxassetid://193424165"
local id3 = "rbxassetid://263562509"
local id4 = "rbxassetid://466347974"
local id5 = "rbxassetid://512934146"

mori = game.Workspace.MORIOHRADIO
game.Workspace.MORIOHRADIO.SoundId = id1
mori:Play()
mori.Ended:Connect(function()
    if game.Workspace.MORIOHRADIO.SoundId == id1 then
        mori.SoundId = id2
        mori:Play()
    elseif game.Workspace.MORIOHRADIO.SoundId == id2 then
        mori.SoundId = id3
        mori:Play()
    elseif mori.SoundId  == id3 then
        mori.SoundId = id4
        mori:Play()
    elseif mori.SoundId == id4 then
        mori.SoundId = id5
        mori:Play()
    elseif mori.SoundId == id5 then
        mori.SoundId = id1
        mori:Play()
    end
end)

But this code is still messy!

What you should do is have a table that contains all the IDs, traverse it, and assign the SoundId to each ID in the table.

Like so:

local soundIds = {
    "rbxassetid://215717985",
    "rbxassetid://193424165",
    "rbxassetid://263562509",
    "rbxassetid://466347974",
    "rbxassetid://512934146"
}

local mori = game.Workspace.MORIOHRADIO

while true do
    for _, soundId in ipairs(soundIds) do
        mori.SoundId = soundId
        mori:Play()
        mori.Ended:Wait()
    end
end

Here's what happens:

  1. Traversing soundIds
  2. Assigning SoundId to whatever soundId is
  3. Play the sound
  4. Wait for it to end
  5. Go onto the next sound ID
  6. Repeat

This discussion is preserved as it was. New answers, comments, and votes are closed.