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

Trouble with looping?

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

✓ Accepted answer

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.

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