Wait() does not wait an amount of time

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

Also here is the video of the wait thing malfunctioning


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

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


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

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


Yes indeed.
Here is the copy of the place.
placeCOPY.rbxl (238.4 KB)
Try to do exactly what I did.
Spawn in the Sovirus outskirts biome, walk to sovirus farmlands, then walk to sovirus outer suburbs.

You can also change your walkspeed if you want.
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

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

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?

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
image

So I decided to print the timeposition of the sound every time the for loop ended, and lo and behold, image
the timepositions is at 22. when it should have been 116

Sometimes, it’s even 8 seconds…
image
(Here is the code I have now btw if you want repro)

local r = {}
local new = false
function sog(s,vol,ye)
    print('jose')
    repeat
        print('sorg')
        for i = 1,#r do
            print(i.." logi")
            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()
            print(tostring(script.Parent.ambience.TimeLength-.5).." COMP "..script.Parent.ambience.TimeLength)
            wait(script.Parent.ambience.TimeLength-.5)
            game:GetService("TweenService"):Create(script.Parent.ambience,TweenInfo.new(.5,Enum.EasingStyle.Quad),{Volume = 0}):Play()
            wait(.5)
            print("ATRUONG "..script.Parent.ambience.TimePosition)
        end
        print('gross')
    until new==true
end
script.Event.Event:Connect(function(s,vol,ye)
    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
        print('raks')
        sog(s,vol,ye)
    end
end)

As expected, it ends the seconds it ended the first time (6 seconds) earlier for the second time
image

Further testing results in the discovery that this problem only occurs on biomes that you did not spawn in.So if you spawned in biome 1, then went to biome 2, this bug would occur.


As you can see here, I spawned in biome 2 and the bug is not there.

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

- Br, iSyriux

1 Like

wait() will not work in RenderStepped or Heartbeat.
Since it runs so often, it’ll wait when starting that function and then run, and that happens so often it doesnt seem like it’s actually waiting.

TLDR: wait() in a RenderStepped or Heartbeat function will not pause the thread like using while true do.

Hello,

So what should I do to fix this?

Replace your RenderStepped function with a while wait() do function

The issue you’re experiencing isn’t because wait() is just being buggy. It’s your optimization. Are you using tons of spawn() functions?

And @infiniteRaymond’s solution will help but the issue will sooner or later be unavoidable.