Wait() function is wrong

Hello,
Here are the only two scripts you need to know.

wait(1)
local oldbiome = 0
local function ta(a,b,c,d,e,f,durat,biome)
	if biome~=oldbiome then
		oldbiome = biome
		game:GetService("TweenService"):Create(game.Lighting.Atmosphere,TweenInfo.new(durat,Enum.EasingStyle.Quad),{Density = a, Offset = b, Color = c, Decay = d, Glare = e, Haze = f}):Play()
		wait(durat)
		game.Lighting.Atmosphere.Density = a
		game.Lighting.Atmosphere.Offset = b
		game.Lighting.Atmosphere.Color = c
		game.Lighting.Atmosphere.Decay = d
		game.Lighting.Atmosphere.Glare = e
		game.Lighting.Atmosphere.Haze = f
	end
end
game:GetService("RunService").RenderStepped:Connect(function()
	local e = game.Workspace.biomes:GetChildren()
	for i, v in pairs(e) do
		if v.Name=="biome" then
			if game.Players.LocalPlayer.Character.Head.Position.X>=v.Position.X-v.Size.X/2 then
				if game.Players.LocalPlayer.Character.Head.Position.X<=v.Position.X+v.Size.X/2 then
					if game.Players.LocalPlayer.Character.Head.Position.Z>=v.Position.Z-v.Size.Z/2 then
						if game.Players.LocalPlayer.Character.Head.Position.Z<=v.Position.Z+v.Size.Z/2 then
							if v.Position.Y>game.Players.LocalPlayer.Character.Head.Position.Y then
								if v.Parent.Parent~=game.Players.LocalPlayer.Character then
									if v.Value.Value==0 then
										script.Value.Value="Wasteland Edges"
										game.Players.LocalPlayer.PlayerGui.ambience.Playing = true
										script.id.Value = v.Value.Value
										local vol = .5
										game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://3097850155"},vol)
										coroutine.wrap(function()
											ta(.1,0,Color3.fromRGB(65, 57, 50),Color3.fromRGB(174, 157, 145),1.05,2.3,1,v.Value.Value)
										end)()
									end
									if v.Value.Value==1 then
										script.Value.Value="Sovirus Outskirts"
										game.Players.LocalPlayer.PlayerGui.ambience.Playing = true
										script.id.Value = v.Value.Value
										local vol = .5
										game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://4172834505"},vol)
										coroutine.wrap(function()
											ta(.1,0,Color3.fromRGB(65, 57, 50),Color3.fromRGB(174, 157, 145),1.32,2.06,1,v.Value.Value)
										end)()
									end
									if v.Value.Value==2 then
										script.Value.Value="Sovirus Farmlands"
										game.Players.LocalPlayer.PlayerGui.ambience.Playing = true
										script.id.Value = v.Value.Value
										local vol = .5
										game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://931256455","rbxassetid://931254912","rbxassetid://935400460","rbxassetid://931260033"},vol)
										ta(.1,0,Color3.fromRGB(175, 207, 255),Color3.fromRGB(225, 215, 198),1.32,2.06,1,v.Value.Value)
									end
									if v.Value.Value==3 then
										script.Value.Value="Sovirus Outer Suburbs"
										game.Players.LocalPlayer.PlayerGui.ambience.Playing = true
										script.id.Value = v.Value.Value
										local vol = .5
										game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://894818144"},{"rbxassetid://1840684551"},vol)
										ta(.1,0,Color3.fromRGB(175, 207, 255),Color3.fromRGB(225, 218, 208),0,0,1,v.Value.Value)
									end
									if v.Value.Value==4 then
										script.Value.Value="Sovirus Suburbs"
										game.Players.LocalPlayer.PlayerGui.ambience.Playing = true
										script.id.Value = v.Value.Value
										local vol = .5
										game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://3182738096","rbxassetid://6062137050","rbxassetid://6062135904"},vol)
										ta(.1,0,Color3.fromRGB(175, 207, 255),Color3.fromRGB(225, 225, 199),0,0,1,v.Value.Value)
									end
								end
							end
						end
					end
				end
			end
		end
	end
end)

Event script:

local r = {}
local new = false

script.Event.Event:Connect(function(s,vol)
	new = false
	if #r~=#s then
		new = true
	else
		for i=1,#r do
			if new==false then
				if r[i]==s[i] then
					new = false
				else
					new = true
				end
			end
		end
	end
	if new==true then
		r = s
		new = false
		repeat
			for i = 1,#r do
				script.Parent.ambience.SoundId = r[i]
				if script.Parent.ambience.IsLoaded==false then
					script.Parent.ambience.Loaded:Wait()
				end
				script.Parent.ambience.Volume = 0
				script.Parent.ambience:Play()
				print(vol)
				game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = vol}):Play()
				wait(script.Parent.ambience.TimeLength-.5)
				game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = 0}):Play()
			end
		until new==true
	end
end)

So this is supposed to


Here is the script where it is event fire

Hierarchy
image
Playtest:


Also another problem here is that my wait is off


This is supposed to be waiting the length of the music minus .5 because that’s the time it takes to fade out tween
Also apparently it’s random. Sometimes it just waits 5 seconds then replays and sometimes it waits like 20 seconds then replays (the audio length is actually more than 100 seconds)
Also another thing to note is that it has 4 audios in its playlist so it’s not supposed to loop around to the first one again when not even waiting the first one’s length

Also another thing to note is that when it prints out the volume it shows that it’s a new biome so yeah
image

This is all very confusing and if I could have some instant help that would be awesome

- Br, iSyriux

1 Like

Hello,
Also here is the video of the wait thing malfunctioning

Hello,
I even printed it out to see the difference and it looks like nothing is wrong here
image
But yet again…what is happening?
image

Hello,
By the way here is the print code if you’re curious


@MrNicNac @sjr04 @loueque @MJTFreeTime could you help here please?

Hello,
I even tried printing “raks” whenever a new loop is introduced and it’s not printed…
It just looks like the wait() is off in the for loop…What the actual hay is goingon?
image

Hello, there is a lot going on here. Would you mind to give me a copy of the place or an isolated model I can load into studio?

1 Like

Hello,
Yes indeed.
Here is the copy of the place.
placeCOPY.rbxl (238.4 KB)

I am not getting the first error with tweenservice. Did you already fix it?

Hello,
Try to do exactly what I did.
Spawn in the Sovirus outskirts biome, walk to sovirus farmlands, then walk to sovirus outer suburbs.

Hello,
You can also change your walkspeed if you want.

Alright, On line 59 of your inbiome script, you have a typo.

game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://894818144"},{"rbxassetid://1840684551"},vol)

It fires the event with (table, table, number) and it expected (table, number). I think you meant:

game.Players.LocalPlayer.PlayerGui.sound.Event:Fire({"rbxassetid://894818144", "rbxassetid://1840684551"},vol)

Hello,
Yup, that’s probably it. My second problem (the wait problem) still needs a solution though.

Correct me if I am wrong. It looks like your sound script, when the event of playing a new song is fired, overwrites the ambience song, plays it, then fades it in, then waits the duration of its current song (the new one being played), then fades it out.

Hello,
Yup, and over and over again for the music table until the music table sent from the event is different from the previous one, in which the loop should stop and a new loop should be made, and so on

Assuming all that code runs as expected, the problem is that the loop does not actually stop. The condition is new == true but the only time this is done it is also instantly set back to false and a new loop is run. The old loop is still on wait() and when it checks new == true, it is always false. Even if this was structured better, toggling a boolean on and off to start and stop loops will always allow multiple loops to be run together if it can be activated frequently enough.

Try doing this instead:

local loopId = 0

function doStuff()
	loopId = loopId + 1
	local id = loopId
	repeat
		
	until id ~= loopId
end

Also the condition of the loop should be checked after every yield so it does not continue functioning if it was replaced during the yield.

local r = {}
local new = false
local loopId = 0


script.Event.Event:Connect(function(s,vol)
	new = false
	if #r~=#s then
		new = true
	else
		for i=1,#r do
			if new==false then
				if r[i]==s[i] then
					new = false
				else
					new = true
				end
			end
		end
	end
	if new==true then
		r = s
		loopId = loopId + 1
		local id = loopId
		
		
		
		if id ~= loopId then return end
		
		while true do
			for i = 1,#r do
				local fadeOut = game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = 0})
				fadeOut:Play()
				fadeOut.Completed:Wait()
				
				if id ~= loopId then return end
		
				script.Parent.ambience.SoundId = r[i]
				if script.Parent.ambience.IsLoaded==false then
					script.Parent.ambience.Loaded:Wait()
				end
				script.Parent.ambience.Volume = 0
				script.Parent.ambience:Play()
				
				game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = vol}):Play()
				wait(script.Parent.ambience.TimeLength-.5)
				
				if id ~= loopId then return end
			end
		end
	end
end)

Hello,

What is “loopId”?

Hello,

I do not understand what your code means. After taking a break of two days, I am quite rusty again on scripting. If you could reiterate where to put this script and what it means, that would be great.

Hello,

Through further testing, it has been revealed that there is actually only one repeat loop running at the time


Otherwise, it would print more than 1 "sorg"s each time

Hello,

It is also noted that there is more than one song in the table. I don’t have any idea why it just completely ends the for loop and begins a new repeat loop.

Hello,

The problem seems to lie in the repeat loop itself.


As you can see here, I have put it in a function and yet it STILL does a wrong wait() time, and restarts the loop even before one song finishes playing… What is going on here?

Hello,

Okay, I began to notice a pattern here… It plays the song once, waits 10-20 seconds then plays it again, then finishes 10-20 early then goes on to the next song in the table, and so on
image
image