Music Regions Are Lagging My Game

I am using “Cindering’s Background Music Toolkit” And This Block Of Code Is Lagging My Game Very Severely.

function RunCycle() --the main cycle which will continuously run, checking which zones (if any) the character is in and playing new music when necessary
local bgm = script:FindFirstChild(“BGM”)
if settings.UseMusicZones == true then
local zone,oldzone = CalculateCurrentZone()
if zone ~= oldzone and zone ~= zoneplayingmusic and bgm then
if (zone == nil and (settings.UseGlobalBackgroundMusic == true or settings.MusicOnlyPlaysWithinZones == true)) or zone ~= nil then
FadeOutSound(bgm)
return
end
elseif zone and bgm == nil then
PlaySound(SelectRandomMusic(zone[“Music”]))
zoneplayingmusic = zone
return
elseif zone == nil and oldzone and settings.MusicOnlyPlaysWithinZones == false and settings.UseGlobalBackgroundMusic == false and bgm == nil then
PlaySound(SelectRandomMusic(oldzone[“Music”]))
zoneplayingmusic = oldzone
return
elseif zoneplayingmusic and settings.MusicOnlyPlaysWithinZones == false and settings.UseGlobalBackgroundMusic == false and bgm == nil then
PlaySound(SelectRandomMusic(zoneplayingmusic[“Music”]))
return
elseif settings.UseGlobalBackgroundMusic == true and bgm == nil then
PlaySound(SelectRandomMusic(music[globali]))
zoneplayingmusic = nil
return
end
elseif bgm == nil and settings.UseGlobalBackgroundMusic == true then
PlaySound(SelectRandomMusic(music[globali]))
return
end

if bgm and (settings.UseGlobalBackgroundMusic == true and zoneplayingmusic == nil and #music[globali] > 1) or (zoneplayingmusic and #zoneplayingmusic["Music"] > 1) then
	local length = bgm.TimeLength
	local pos = bgm.TimePosition
	if length ~= 0 and length - pos < FadeoutTime + .5 then
		FadeOutSound(bgm)
	end
end

end

while wait(2) do
RunCycle()
end

I Just Need Help With Cutting Down The Calculations Because It Is Constantly Checking All The Music Regions And The Character Every 2 Seconds Which Causes Tons Of Lag

Not meaning to change your system but I recommend using this module, it’s awesome for what you need.

1 Like

thanks ill try it out right now

Are you using Region3 or Touched event?