Avoiding/More reliable wait() on the Server

Now before you get mad at me, usually I would use RenderStepped or some other event of RunService to calculate time or small yields, but since these are unusable I’ve come up with an almost bootleg solution using events that can be used on the server side. I really don’t want to use this because, to be frank, it seems stupid. But I don’t know of any other reliable waiting methods that can be ran on the server. If you do, please let me know so I can start using it. Or if you have any other methods that you think would work better, please feel free to share them with me and others.

local tService=game:GetService("TweenService")
local waitInt=game:GetService("ServerStorage"):WaitForChild("WaitInt")

local function myWait(n)
local tinfo=TweenInfo.new(n);local timeTween=tService:Create(waitInt,tinfo,{Value=n})
timeTween:Play(); timeTween.Completed:Wait()
end
--Ignore syntax or capitalization errors, I wrote this on the DevForum not in studio
1 Like

Um, why are they unusable exactly? You can still use RunService events on server (Except for RenderStepped and PreRender, of course).

This is the function I use. It waits until the time is almost up to actually start counting frames, since there’s an unacceptable processing time cost when you have 60 scripts calling rs.Heartbeat:Wait().

local rs = game:GetService("RunService")
function counterWait(t)
	local startTime = tick()
	local elapsed = tick() - startTime
	local timeToCompletion = t - elapsed
	while elapsed < t do
		if timeToCompletion > 4 then
			wait(1)
		else
			rs.Heartbeat:Wait()
		end
		elapsed = tick() - startTime
		timeToCompletion = t - elapsed
	end
end
1 Like

What I use is make a variable for “timePassed”, and then count that up every Heartbeat until it is past the requested wait time.

I was under the impression that all runservice events that run each frame, Heartbeat, Stepped, RenderStepped, etc, only ran on the client side. Is this not the case?

No, because server can and does calculate physics for the unanchored parts they have the network ownership of too. So Heartbeat and Stepped events are useable on the server.

1 Like
local function wait(n: number?) -- Better wait function
	if not n or n == 0 then
		return RunService.Heartbeat:Wait()
	else
		local timePassed = 0
		
		while timePassed < n do
			timePassed += RunService.Heartbeat:Wait()
		end
		
		return timePassed
	end
end

Here’s a snippet I use.

(RunService has to be declared before, of course)

Whoops. The more you know :sweat_smile: