Music script not looping?

I am currently fixing my music script as it was playing all the sounds at once, however, It’s not looping properly. For example

_G.changed = true
local db = false
local numofsounds = 0

local function LoopMusic()
	numofsounds = 0
	db = false
	wait(1)
	if db == false then
		wait()
		db = true
		for i, v in pairs (game.Lighting:GetChildren()) do
			if v:IsA('Sound') then
				numofsounds = numofsounds + 1
			end
		end
		for i = 1, numofsounds do
			game.Lighting["Sound"..i]:Play()
			wait(tonumber(game.Lighting["Sound"..i].TimeLength))
		end
		numofsounds = 0
		db = false
	end
end

while wait() do
	if _G.changed == true then
		_G.changed = false
		LoopMusic()
	end
end

In the output it only prints “ChangedG” once and not multiple times at it is meant to be due to it being looped, any fixes for this?

–EDIT: all the music inside of the folder is now being played

That’s probably because you’re not changing _G.changed back to true after loopMusic is done executing so it’s only gonna loop once.



while wait() do
	if _G.changed == true then
		_G.changed = false
		LoopMusic()
                _G.changed = true
	end
end

Also I don’t really recommend using the _G table, using bindable events or value objects for example.

It cannot be that because i moved it into another script which removes the requirement for _G, right now it is playing all sounds within the folder at once, the i value at the for i loop seems to be equals to the numofsounds too,

so when i run through the for i loop it’s as if it ignores the wait() line

Nevermind, i fixed it I just stopped all the sounds that were in lightning then added a repeat wait until the timelength was not equal to 0 as it was not loaded properly for whatever reason causing it to skip the wait()

1 Like