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

Issue with loops and conditions?

lobbymusic = script:WaitForChild("lobbymusic")
print("Got table 'Lobbymusic!'")
intermusic = script:WaitForChild("intermusic")
print("Got table 'intermusic!'")
local replicatedstorage = game:GetService("ReplicatedStorage")
print("Got replicatedstorage!")
local statustag = replicatedstorage:WaitForChild("statustag")
print("Got statustag!")
local electro = script.lobbymusic:WaitForChild("electro")
print("Got electro!")
local motivational = script:WaitForChild("motivational")
print("Got m1!")
local rogue = script:WaitForChild("rogue")
print("Got m2!")
local spyro = script.intermusic:WaitForChild("spyro")
print ("Got spyro!")
local playing = script:WaitForChild("musicplaying")
print("Got boolean 'Playing!'")
local type = script:WaitForChild("musictype")
print("Got string 'musictype!'")
lobbytable = lobbymusic:GetChildren()
print("Got children of table 'Lobbymusic!'")

while true do
	wait()
	if playing.Value == true then
		print("Music is already playing!")
		while true do
			wait()
			if statustag.Value == "Intermission" or statustag.Value == "Game in progress" or statustag.Value == "Loading map" then
				playing.Value = false
				playmusic:Stop()
				print("Stopped music.")
				break
			end
		end
	elseif playing.Value == false then
		if statustag.Value == "Error: Not enough players! Invite a friend to start playing!" or statustag.Value == "The game will start soon" and playing.Value == false then
			type.Value = "lobby"
			print("The music is switching to type 'lobby'")
			playing.Value = true
			playmusic = lobbytable[math.random(1,#lobbytable)]
			playmusic:Play()
			for vol = 0,0.6,0.05 do
				wait(0.2)
				playmusic.Volume = vol
			end
		if statustag.Value == "Intermission" and playing.Value == false then
			wait()
			type.Value = "intermusic"
			print("The music is switching to type 'intermission'")
			playing.Value = true
			playmusic = intermusic[math.random(1,#intermusic)]
			playmusic:Play()
			for vol = 0,0.6,0.05 do
				wait(0.2)
				playmusic.Volume = vol
			end
		end
		end
	end
end

The problem is that I've made a while true do loop in which it's constantly checking for certain conditions. The problem is that once the conditions are met, it continuously runs the code because the conditions are being recreated.

Basically when the value of playing is true and the value of statustag == "Intermission" then it runs that code.

I understand the problem but I do not know how to fix it, so help would be greatly appreciated. Thank you!

1 comment

0

You do not need 2 Loops.

1 answer

✓ Accepted answer

This might work but I cannot test it without your project files

        while  statustag.Value ~= "Intermission" or statustag.Value ~= "Game in progress" or statustag.Value ~= "Loading map" do
		wait(0.2)
            
        end
                playing.Value = false
                playmusic:Stop()
                print("Stopped music.")

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