Are my using RunService.Stepped correctly?

Im trying to wait the delay but because of server lag the waits are a lot longer than they should be.

Current Code for wave system

function WaveManager.SpawnWave(Wave)
		local WaveContent = Waves.CalcWave(Wave.Value)
			for _, WaveData in ipairs(WaveContent) do
				local Choice = math.random(1, #WaveContent)
				local WaveInfo = WaveContent[Choice]
					for Clones=1,WaveInfo.Amount do
						wait(WaveInfo.Delay)
						local Clone = game.ServerStorage.Bloons[WaveInfo.Bloon]:Clone()	
						Clone.Parent = workspace.Bloons
						Clone:SetPrimaryPartCFrame(game.Workspace.Map.Start.CFrame + Vector3.new(0,1,0))
						Clone.Runner.Disabled = false
					end
				end
			local Money = 50 * Wave.Value / 2
			game.ServerScriptService.Events.AddMoney:Fire(Money)
			--wait(WaveContent[1].WaveDelay)
                        RunService.Stepped:Wait(WaveContent[1].WaveDelay) -- There appears to be no wait at all
			Wave.Value = Wave.Value + 1
		end

RunService.Stepped:Wait(WaveContent[1].WaveDelay) does not seem to be waiting at all.
Any tips on using this and making this faster?

1 Like

The Event:Wait method doesn’t take any arguments. It just wants for the event to fire. E.g. you could do

local character = localPlayer.Character or localPlayer.CharacterAdded:Wait()

to wait for the players character to load. If you want to wait for 1 frame, you do

RunService.RenderStepped:Wait()

If you want to wait for a number of seconds, just use wait

But with server lag it slows the game down.

If you do RunService.Stepped:Wait() all you are doing is waiting for the physics before a frame to occur. To my trained eye, that is literally pointless as if as you said the game lags then you might wait a bit longer. If you have a lot to load before the frame and lots of physics need calculating then this function would slow dramatically.

You would be better just using wait() or nothing at all - you’ll see little difference in this function. If you have nothing it will just seem heavier.

If you want for it to delay until an event occurs then you should create a conditional loop until the arguments are met.

If you want for it to delay for a periodic time then simply do:

local start = tick()
delay = 5.23

repeat wait() until tick() - start >= delay

That way it waits a specific amount of time before continuing.

2 Likes

while tick() - start < delay do end would be better(for small delay values) as wait() can often be inconsistent, and may yield the script for slightly longer than required.

1 Like

Here is a function that can replace wait() cause wait() does wait longer, and can wait even longer than needed depending on throttling conditions, which can make it wait a lot longer depending on how much you use it.

local function CustomWait(n)
	n = n or 0
	local DeltaTime = 0
	repeat
		DeltaTime = DeltaTime + RunService.HeartBeat:Wait()
	until DeltaTime > n
	return DeltaTime
end
2 Likes