Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My random picking music script isn't working correctly?

Asked by
Relatch 550 Moderation Voter
10 years ago

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
0
Is it in a regular script? Redbullusa 1580 — 10y
0
Yes. Relatch 550 — 10y

3 answers

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
10 years ago

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"

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 Shawnyg 4330 — 10y
0
I wouldn't understand that because it would act as if it were words, correct? Relatch 550 — 10y
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. Discern 1007 — 10y
Ad
Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
10 years ago

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
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. Discern 1007 — 10y
0
I remember once when I tried something similar to his original, it resulted in an error. Shawnyg 4330 — 10y
0
Nevermind, worked perfectly fine. Relatch 550 — 10y
Log in to vote
-1
Answered by 10 years ago
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!

0
Workspace.MusicController:5: attempt to concatenate field '?' (a nil value) Relatch 550 — 10y

Answer this question