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

My random picking music script isn't working correctly?

Well, the music plays only once for 100 seconds, then it doesn't choose music again. I don't know if I did something wrong or not, please help me. It also doesn't error when it's looped a full 100 seconds.

music = {176407800, 166886402, 183503743, 185698490, 165065112, 145127587, 155146819, 178066730, 137985283}

while true do
    local id = math.random(1, #music)
    game.Workspace.Sound.SoundId = "rbxassetid://" .. music[id]
	game.Workspace.Sound:Play()
    wait(100)
	game.Workspace.Sound:Stop()
end

2 comments

0

Is it in a regular script?

0

Yes.

3 answers

✓ Accepted answer

The numbers in the table on line 1 must be strings, since SoundId is a string, Put quotation marks around each value in the table, and you will be good to go.

For example: 176407800 should look like this "176407800"

3 comments

0

They wouldn't necessarily need to be in quotation marks. Test it out for yourself, use concatenation to put in a variable with the value of "2" (No quotes), it works fine

0

I wouldn't understand that because it would act as if it were words, correct?

0

Incorrect, a string is a sequence of letters, numbers, and symbols. Strings are not only restricted to alphabetic letters, they include any character possible. You cannot combine with a string with an integer, so you make the integer a string by placing quotation marks around each value. This allows the function of combining strings with strings.

Well, you messed up on line 4. You almost had it, though. When getting a random parameter from a table, you have to use the template, which is provided in line 4.

music = {176407800, 166886402, 183503743, 185698490, 165065112, 145127587, 155146819, 178066730, 137985283}

while true do
    local id = music[math.random(1, #music)]
    game.Workspace.Sound.SoundId = "rbxassetid://" .. music[id]
    game.Workspace.Sound:Play()
    wait(100)
    game.Workspace.Sound:Stop()
end

3 comments

0

If you look on line 5 though, he used music[id], not just "id" so the original script is actually correct. Your script is trying to select a random value from an integer value that was already chosen in Line 4, resulting in an error.

0

I remember once when I tried something similar to his original, it resulted in an error.

0

Nevermind, worked perfectly fine.

while true do
local h = Instance.new("Hint")
h.Parent = game.Workspace
h.Text = "Game by JustGimmeDaBux. PM JustGimmeDaBux for Information!"
wait(7)
h.Text = "Remember: Griefing, C4, AND flooding are STRICTLY PROHIBITED."
wait(7)
h.Text = "Found an Exploiter? PM JustGimmeDaBux the FULL USERNAME with PROOF!!"
wait(7)
h.Text = "Thanks for playing! Leave a like/comment. Keep building!"

Above is my script. Yours looks like you're trying to do the same thing. Your script below looks fine, but this may work to improve the script.

music = {176407800, 166886402, 183503743, 185698490, 165065112, 145127587, 155146819, 178066730, 137985283}

while true do
    local id = music[math.random(1, #music)] -- Error on this line
    game.Workspace.Sound.SoundId = "rbxassetid://" .. music[id]
    game.Workspace.Sound:Play()
    wait(100) -- You could lower the time because some songs may not have a loop and keep going through blank song until the 100 seconds are up.
    game.Workspace.Sound:Stop()
end -- Before your end, you should add several and then add a repeat script so it randomly repeats the music so your viewers don't run out of music to listen to.

Hope I helped!

1 comment

0

Workspace.MusicController:5: attempt to concatenate field '?' (a nil value)

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