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

Trouble with looping?

Asked by 9 years ago

So basically I have this game round script that loops and I'm trying to make it choose a random song for the intermission. The problem is that the script chooses a song then automatically chooses another one without going through the round.

Here is the script:

local replicatedstorage = game:GetService("ReplicatedStorage")
local status = replicatedstorage:WaitForChild("StatusValue")

while true do
-- Intermission
    for i = 30,0,-1 do
        status.Value = "INTERMISSION: "..i
        wait(1)
        Song = Instance.new("Sound")
Song.Parent = workspace
Song.Volume = 1
Song.Looped = false
Assets = {274504661,263966756,346191865}
for _, asset in ipairs(Assets) do
game:GetService("ContentProvider"):Preload("http://www.roblox.com/asset/?id=" .. asset)
end
Song.SoundId = "http://www.roblox.com/asset/?id=" ..Assets[math.random(1,#Assets)]
Song:Play()
    end

-- Round
for i = 100,0,-1 do
    status.Value = "TIME LEFT: "..i
    wait(1)
end
status.Value = "GAME OVER"
wait(1)
end

1 answer

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

I see your issues. You have a the sound, in a for loop, inside of a while loop. You never closed line 6 (or line 14) until line 19. That means each time the loop goes through, it'll run the code again. Since I can't remember the exact rule for 'end', I can't tell you which for loop it's in. But, I recommend putting line 17-19, and move it to line 20, where it's outside of those loops.

Ad

Answer this question