Module Script Can't do wait() function

I’ve been trying to improve various portions of code I’ve wrote, trying to use Module Scripts to reduce the amount of spaghetti code but this atrocious wait function I tried to use simply refuses to work:

function functions.wait(n)
    local elapsed = 0
    if n == nil then n = 0 end

	repeat
		elapsed = elapsed + game:GetService("RunService").Heartbeat:Wait() --Why doesn't this work!?
    until elapsed >= n
    return elapsed
end

Every time a script calls that function from the module script, it returns an error like this:
ServerScriptService.Functions:12: attempt to compare table and number

I’ve tried using other methods such as tick() and os.clock() and they all have the same result.
The function works 100% when it isn’t in the module script however which is annoying and not what I want.

How am I supposed to fix this without copying the function to every single server script individually.

ServerScriptService.Functions:12: attempt to compare table and number

It’s because you’re calling the function as function:wait(n) so it passes the first argument as self implicitly but you can’t use self because of the way you defined the function so the first argument is passed as a table which is self, due to which it is errors. Call the function as functions.wait(n).

2 Likes