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

Can you explain to me why my music isnt playing a new one each time?

I want it to play a different one than the one it played previous. How do I do this?

--AllianceScripter's Radio--

local music = script.Parent
local gui = script.Parent.Parent
local talk = script.Parent.Parent.Talk
local justplayed = {}

local musicids = {181547615,142376088}

while true do wait()
	local ran = (musicids[math.random(1,2)])
		music.SoundId = "http://www.roblox.com/asset/?id="..ran
		table.remove(musicids,ran)
		table.insert(justplayed,ran)
		music:Play()
		while true do wait()
			if talk.IsPlaying == false then
				wait(120)
			for i = 0.5,0,-0.1 do
				music.Volume = i
				wait(0.01)
			end
				music:Stop()
				wait(2)
				music.Volume = 0.5
				music:Play()
				table.remove(justplayed,ran)
				table.insert(musicids,ran)
			end
		end
	end

1 answer

Firstly, don't remove any of the sound IDs from the musicids table, it's unnecessary. Also, don't make the justplayed variable a table, it's simpler to just put the ID in.

Secondly, you could do a repeat loop to keep setting ran to a random music id until ran is not equal to the id of what just played.

Like so:

--AllianceScripter's Radio--

local music = script.Parent
local gui = script.Parent.Parent
local talk = script.Parent.Parent.Talk
local justplayed --Kept blank so a random song can run first before the just played song is set.

local musicids = {181547615,142376088}

while true do wait()
    local ran = musicids[math.random(#musicids)] --Doing math.random(#musicids) is much easier as you don't have to edit the numbers every time you change the table.
	repeat ran = musicids[math.random(#musicids)] until ran ~= justplayed --Keep setting ran to a random music id until it doesn't equal justplayed.
        music.SoundId = "http://www.roblox.com/asset/?id="..ran
        justplayed = ran
        music:Play()
        while true do wait()
            if talk.IsPlaying == false then
                wait(120)
            for i = 0.5,0,-0.1 do
                music.Volume = i
                wait(0.01)
            end
                music:Stop()
                wait(2)
                music.Volume = 0.5
                music:Play()
            end
        end
    end

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