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

Studio Glitch Help with Music and Sound Audios?

Asked by 7 years ago
Edited 7 years ago

This script is placed inside a model in workspace called MusicHolder along with some audios. Why does it work well in studio and choose random and different songs in studio, but in-game it chooses to play the same song over and over. The script :


print('ran') local musicholder = script.Parent local songList = { musicholder:WaitForChild('CW'), musicholder:WaitForChild('DeckDubstep'), musicholder:WaitForChild('EhDe'), musicholder:WaitForChild('JayKode'), musicholder:WaitForChild('Ookay'), musicholder:WaitForChild('SantaComes'), musicholder:WaitForChild('XmasToMe'), --musicholder:WaitForChild('Song'), --musicholder:WaitForChild('Song'), --musicholder:WaitForChild('Song') -- we wait for each song -- add the rest of the songs to this list } local songCount = #songList -- this returns the number of items in the table above so we only change as little code as possible when adding songs while true do wait(3) local songNum = math.random(1, songCount) -- gets a random number in the range of the number of songs print(songNum) songList[songNum]:Play() -- we access the sound in the table and play it songList[songNum].Ended:Wait() -- we wait until the song has finished then play anther song end

3 answers

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

Random numbers are generated using a seed. By changing this seed, you can change the numbers generated. In order to change it, you'll need math.randomseed:

print('ran')
local musicholder = script.Parent
math.randomseed(tick());

local songList = {
    musicholder:WaitForChild('CW'),
    musicholder:WaitForChild('DeckDubstep'),
    musicholder:WaitForChild('EhDe'),
    musicholder:WaitForChild('JayKode'),
    musicholder:WaitForChild('Ookay'),
    musicholder:WaitForChild('SantaComes'),
    musicholder:WaitForChild('XmasToMe'),
    --musicholder:WaitForChild('Song'),
    --musicholder:WaitForChild('Song'),
    --musicholder:WaitForChild('Song')
    -- we wait for each song
    -- add the rest of the songs to this list
}
local songCount = #songList -- this returns the number of items in the table above so we only change as little code as possible when adding songs

while true do
    wait(3)
    local songNum = math.random(1, songCount) -- gets a random number in the range of the number of songs
    print(songNum)
    songList[songNum]:Play() -- we access the sound in the table and play it
    songList[songNum].Ended:Wait() -- we wait until the song has finished then play anther song
end

We use tick because tick is a function which returns the amount of time since the UNIX epoch. Since time is always changing, we can be sure that it will be a different number each time the game runs.


As an added measure, you can make sure you're choosing a different song than the song that just played easily:

print('ran')
local musicholder = script.Parent
math.randomseed(tick());

local songList = {
    musicholder:WaitForChild('CW'),
    musicholder:WaitForChild('DeckDubstep'),
    musicholder:WaitForChild('EhDe'),
    musicholder:WaitForChild('JayKode'),
    musicholder:WaitForChild('Ookay'),
    musicholder:WaitForChild('SantaComes'),
    musicholder:WaitForChild('XmasToMe'),
    --musicholder:WaitForChild('Song'),
    --musicholder:WaitForChild('Song'),
    --musicholder:WaitForChild('Song')
    -- we wait for each song
    -- add the rest of the songs to this list
}
local songCount = #songList -- this returns the number of items in the table above so we only change as little code as possible when adding songs
local played = {}; --// Create a table for played songs.

while true do
    wait(3)
    local songNum = math.random(1, songCount) -- gets a random number in the range of the number of songs

    if played[songNum] then --// Check if the song has been played.
        repeat songNum = math.random(songCount) until not played[songNum];
        --// Choose a new song.
    end;
    played[songNum] = true; --// Record that this song has been played.
    if #played == songCount then
        played = {}; --// If all of them have been played, reset the table.
    end;

    print(songNum)
    songList[songNum]:Play() -- we access the sound in the table and play it
    songList[songNum].Ended:Wait() -- we wait until the song has finished then play anther song
end

Hope this helped.

PS: I did it for you this time, but in the future please be sure to properly indent your code; this makes it easier to read for anyone trying to help you out.

Ad
Log in to vote
0
Answered by 7 years ago

You could've just read this xD link

math.randomseed(tick())
print('ran')
local musicholder = script.Parent

local songList = {
musicholder:WaitForChild('CW'),
musicholder:WaitForChild('DeckDubstep'),
musicholder:WaitForChild('EhDe'),
musicholder:WaitForChild('JayKode'),
musicholder:WaitForChild('Ookay'),
musicholder:WaitForChild('SantaComes'),
musicholder:WaitForChild('XmasToMe'),
--musicholder:WaitForChild('Song'),
--musicholder:WaitForChild('Song'),
--musicholder:WaitForChild('Song')
-- we wait for each song
-- add the rest of the songs to this list
}
local songCount = #songList -- this returns the number of items in the table above so we only change as little code as possible when adding songs

while true do
wait(3)
local songNum = math.random(1, songCount) -- gets a random number in the range of the number of songs
print(songNum)
songList[songNum]:Play() -- we access the sound in the table and play it
songList[songNum].Ended:Wait() -- we wait until the song has finished then play anther song
end

Log in to vote
0
Answered by 7 years ago

In game, open the developer console and see which value songNum is being printed. If it is the same one, try math.randomseed()

0
ok thx Inpolite 44 — 7y

Answer this question