How can I return in leftClick() function?

I want to create a radio, which will pick random songs, and when they finish, it will return to the picking state.
Probloem is, it doesn′t return… How can I make it return?

local button = script.Parent

local function leftClick()
	script.Parent.Parent.Parent.Namus.Text = "RockFM"
	script.Parent.Parent.Parent.Pause.Text = "▮▮"
	local random = math.random(1,7)
	if random == 1 then
		print("1Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://14233703514"
		script.Parent.Parent.Parent.Sound:Play()
		wait(213.42)
		print("returnd")
		return
	end
	if random == 2 then
		print("2Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://14180233202"
		script.Parent.Parent.Parent.Sound:Play()
		wait(233.653)
		print("returnd")
		return
	end
	if random == 3 then
		print("3Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://14251338011"
		script.Parent.Parent.Parent.Sound:Play()
		wait(201.142)
		print("returnd")
		return
	end
	if random == 4 then
		print("4Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://14037889093"
		script.Parent.Parent.Parent.Sound:Play()
		wait(168.426)
		print("returnd")
		return
	end
	if random == 5 then
		print("5Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://13519287633"
		script.Parent.Parent.Parent.Sound:Play()
		wait(224.571)
		print("returnd")
		return
	end
	if random == 6 then
		print("6Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://13712572191"
		script.Parent.Parent.Parent.Sound:Play()
		wait(215.431)
		print("returnd")
		return
	end
	if random == 7 then
		print("7Rock")
		script.Parent.Parent.Parent.Sound.SoundId = "rbxassetid://13499683535"
		script.Parent.Parent.Parent.Sound:Play()
		wait(10) --This is the currently tested audio.
		print("returnd")
		return
	end
end

button.MouseButton1Click:Connect(leftClick)
1 Like

If you’re trying to play another song after the current song ends instead of having the radio end with the song, try calling leftClick() again before the ‘return’ keyword in each of the if statements.

Also, you might wanna try replacing the wait() calls with script.Parent.Parent.Parent.Sound.Ended:Wait() instead. The function seems to stop before the sound is finished.

You may call the function instead of return but it may cause stack overflow.

local IDsTable = {
	"rbxassetid://14233703514",
	"rbxassetid://14180233202",
	"rbxassetid://14251338011",
	"rbxassetid://14251338011",
	"rbxassetid://13519287633",
	"rbxassetid://13519287633",
	"rbxassetid://13499683535"
}
function PlaySound()
	local ChosenSoundID = IDsTable[math.random(1,#IDsTable)]
	local Sound:Sound? = script.Parent.Parent.Parent.Sound
	Sound.SoundId = ChosenSoundID 
	Sound:Play()
	Sound.Ended:Wait()
	return -- Ended?
end
1 Like