Avoiding wait() and why

Creating a new instance isn’t expensive, so there is no point.

local before = os.clock()

for i = 1, 1000 do
	Instance.new("Part", workspace)
end

print(os.clock() - before)

After a 1000 iterations: 0.0111

Just a reminder to not use the second parameter of Instance.new() to set the Parent.

This comes with a citation, only if you aren’t setting the properties of an instance. In the code, I’m not setting any properties so there is no problem with using the second parameter of Instance.new.

6 Likes

That’s a really interesting way of optimizing code. I’ll try it out soon enough.

Sorry for bumping again

How would I replace this piece of code?

local c = 240

	coroutine.wrap(function() 
		repeat wait(1)
			c -= 1
		until c == 1
	end)()

repeat wait(1) until c == 1 or ReplicatedStorage.Values.PlayersLeft.Value == 1
local ready = Instance.new("BindableEvent")

delay(function()
    ready:Fire()
end, 240) -- I forget if it's delay(240, function() end)

ReplicatedStorage.Values.PlayersLeft.Changed:Connect(function(newValue)
    if newValue == 1 then
        ready:Fire()
    end
end)

ready.Event:Wait()
2 Likes

looked it up to make sure and yes you put the time first

I agree with this, but what about using it in the context of wait(5)? Do I have to create an intricate RunService.Heartbeat:Wait() solution such that I repeat that 300 times?

It says right at the top of the post.

Let’s talk about wait() . Not wait(n) (although if you are using small n values to where it’d be barely distinguishable from wait() , you’re probably going to hit the same issues mentioned in this thread).

you do know frame rate is always different making this bad practice

Ah okay thank you!

Also, I believe it is Delay(time, function)

yep that’s exactly what I said to them as well

1 Like

Luckily, you can use ModuleScripts so that you only write the code once.

May I know why are we returning these 2 values?

But I thought you said not to use wait() ever?

Please read the post. wait() is bad, but mostly because it’s doing long polling when you should be reacting to events.

So polling on events = bad

But on other cases, it’s good?

I’ve collected all my thoughts on this in the original thread.

1 Like

I’m sorry but I still have some questions in my head I’m just asking if wait() is really useless or useful in certain cases?

Oh my god am I a necrobumpers :sob:

Anyways, is ‘delay’ unreliable? Just wondering :smiley: