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

I'm trying to put music in my game, but it plays all three sounds at once. How do I fix this?

Asked by 5 years ago
while true do
    wait(2)
    script.Parent.Selection.Value = math.random(1,3)
    if script.Parent.Selection.Value == 1 then
        script.Parent.Part.Music:Play()
    end
    if script.Parent.Selection.Value == 2 then
        script.Parent.Part.Music2:Play()
    end
    if script.Parent.Selection.Value == 3 then
        script.Parent.Part.Music3:Play()
    end
end

4 answers

Log in to vote
0
Answered by 5 years ago

So what your cod is doing is:

--Ok wait 2 seconds
--Pick a random number
--Whatever the number is play a sound according to it
--Do that again

So every 2 seconds a new sound plays. To fix this you could do something like this:

local CurrentSong

while true do
    wait(1)
    CurrentSong = math.random(1, 3)

    if CurrentSong == 1 then
        CurrentSong = script.Parent.Part.Music
    elseif CurrentSong == 2 then
        CurrentSong = script.Parent.Part.Music2
    else
        CurrentSong = script.Parent.Part.Music3
    end

    CurrentSong:Play()
    CurrentSong.Ended:Wait()
end
Ad
Log in to vote
0
Answered by 5 years ago

use the property .TimeLength then wait

--example
while true do
    wait(2)
    script.Parent.Selection.Value = math.random(1,3)
    if script.Parent.Selection.Value == 1 then
        script.Parent.Part.Music:Play()
wait(script.Parent.Music.TimeLength+1)
script.Parent.Part.Music:Stop()
    end
    if script.Parent.Selection.Value == 2 then
        script.Parent.Part.Music2:Play()
wait(script.Parent.Music2.TimeLength+1)
script.Parent.Part.Music2:Stop()
    end
    if script.Parent.Selection.Value == 3 then
        script.Parent.Part.Music3:Play()
wait(script.Parent.Music3.TimeLength+1)
script.Parent.Part.Music3:Stop()    

end
end

0
LOL THATS HARDCODED AF Psudar 882 — 5y
0
lmao i like to keep my stuff compact EmbeddedHorror 299 — 5y
0
and i didnt feel like making a new script so i just edited his lol EmbeddedHorror 299 — 5y
Log in to vote
0
Answered by
Psudar 882 Moderation Voter
5 years ago

What you want to do is pick a song and then wait its entire length.

This is probably the simplest way to do it:

while true do
    script.Parent.Selection.Value = math.random(1,3)
    if script.Parent.Selection.Value == 1 then
        script.Parent.Part.Music:Play()
    script.Parent.Part.Music.Ended:Wait()
    elseif script.Parent.Selection.Value == 2 then
        script.Parent.Part.Music2:Play()
    script.Parent.Part.Music.Ended:Wait()
    elseif script.Parent.Selection.Value == 3 then
        script.Parent.Part.Music3:Play()
    script.Parent.Part.Music.Ended:Wait()
    end
end

This will halt the loop until a new song can be chosen after the chosen song has initially finished.

Log in to vote
0
Answered by 5 years ago

The problem with the responses above is that you'd have to keep adding onto the script for every song you add to the part. This script will play through every song in the part and you won't need to add code to the script:

SongPart = script.Parent -- Assuming that this script is inside the part with multiple songs

while wait() do -- Looping infinite amount of times
    for i, song in pairs(SongPart:GetChildren()) do -- looping through objects in SongPart
        if song:IsA('Sound') then -- If the object in SongPart is a "Sound" instance, then:
            song:Play() -- Plays song
            song.Ended:wait() -- Wait for song to end
        end
    end
end

Here's another script that will play the songs at random:

SongPart = script.Parent -- Assuming that this script is inside the part with multiple songs
SongTable = {} --  creating a table for all the songs from SongPart

for i, song in pairs(SongPart:GetChildren()) do -- Looping through objects in SongPart
    if song:IsA('Sound') then -- If the object in SongPart is a sound instance, then:
        table.insert(SongTable, #SongTable + 1, song) -- insert song in SongTable
    end
end

while wait() do
    local RandomSong = SongTable[math.random(1, #SongTable) ] -- Sets a random song

    RandomSong:Play() -- plays the random song chosen
    RandomSong.Ended:wait() -- waits until song is finished playing
end

NOTE: the :IsA() function is only necessary if you have any children within the part that ISN'T a sound instance.

Answer this question