How would i skip waiting for a function to finish?

If i have a loop and inside of it, i have a function. say something like this|:

function functioname()
wait(5)
end

while wait() do
functionname()
end

how would i make it so that, I don’t have to wait the 5 seconds for the function to finish to proceed with the loop?

2 Likes

You could use delay(), spawn(), or coroutines.

3 Likes

Wrap it with a c-c-c-couroutinee :sunglasses:

Edit: How to do it.

coroutine.wrap(function()
    -- loop here
end)()

-- rest of code

What this does it makes it a sub event which will run the loop while executing the rest of your code.

10 Likes

I wouldn’t recommend coroutines unless you know what you’re doing. Spawn or delay should work just fine.

Spawn usage:

spawn(function, ...) calls function in the background with the arguments ... (can be nothing) as soon as possible.

spawn(function()
    -- background work
end)
-- or
spawn(functioname) -- that is how you spelled it in the post :)

delay(delay, function, ..) calls function in the background with the arguments ... (can be nothing) after delay seconds.

delay(0, function()
    -- background work
end)
-- or
delay(0, functioname) -- that is how you spelled it in the post :)

Don’t rely on spawn to run immediately. Don’t rely on delay to be precise. The only thing spawn guarantees is that your function will run as soon as possible. The only thing delay guarantees is that your function will be run as soon as possible after delay seconds have passed.

You might want to test if the ... works though, I haven’t tested it recently and the docs don’t mention it.

8 Likes

@Paralusuz @b_oolean @Terrodactyl
I am trying to use it with my code, however im not to sure how to apply it in my case:

coroutine.wrap(function()
while wait(.1) do
	for i, v in pairs(items:GetChildren()) do
		if not v.PrimaryPart:FindFirstChild("IsGrabbed") and not v:FindFirstChild("WeldTo") then
		if (RespawnInfo[v] - v.PrimaryPart.Position).Magnitude >= respawndist then
			TimerInfo[v] = TimerInfo[v] + 0.1
		end
		
		if TimerInfo[v] >= respawntime then
			Respawn(v, RespawnInfo[v])
			TimerInfo[v] = 0
		end
		else
		TimerInfo[v] = 0
		end
	end
end
end)

I need the “Respawn” Function to not slow down the loop from continuing however what i did yields no results.

Sorry if this is really easy I just haven’t ever worked with coroutines, spawn or delay

You forgot to call it at the end, add () after the bottom most line and see what happens.

coroutine.wrap(...) creates a function that you then have to call to start the coroutine. That’s why you need to go coroutine.wrap(...)().

Spawn is somewhat simpler, and I wouldn’t use coroutine.wrap unless I had a good reason to.

1 Like

Adding onto @anon66957764 's response, you need to call the method so make sure the 3rd end is end)().

You put the code you DONT WANT TO WAIT FOR i.e a loop in the couroutine so it runs in the background, essentially a simple explanation.

2 Likes

Yeah. If your loop also runs inside the coroutine, then the coroutine will be waiting for the code inside the loop, and it will be just as slow.

You could start the coroutine inside the loop, and then the loop will always run at a constant speed no matter how long the code inside takes (because it gets run in the background). Be cautious of this because if your code runs multiple instances of itself at once, weird things can happen.

1 Like

@b_oolean @Terrodactyl

coroutine.wrap(function()
while wait(.1) do
	for i, v in pairs(items:GetChildren()) do
		if not v.PrimaryPart:FindFirstChild("IsGrabbed") and not v:FindFirstChild("WeldTo") then
		if (RespawnInfo[v] - v.PrimaryPart.Position).Magnitude >= respawndist then
			TimerInfo[v] = TimerInfo[v] + 0.1
		end
		
		if TimerInfo[v] >= respawntime then
			Respawn(v, RespawnInfo[v])
			TimerInfo[v] = 0
		end
		else
		TimerInfo[v] = 0
		end
	end
end
end)()

I’ve added it, am i meant to add the function in the brackets?
And spawn has an in-built wait right? that’s why i don’t want to use it

ps: it’s still doing the same thing as before, waiting for the function to finish.

1 Like

You should always go for a coroutine rather than a spawn().

Spawn was called evil because when used mutliple times throughout a script, you recieve a significant amount of lag and sometimes even delayed execution.
knowledge bro: Coroutines V.S. Spawn()... Which one should I use?

2 Likes

Yes never never!!1 avoid using spawn, coroutine mega cool and many benefits.

Yeah looks right, no need to add the function name within calling it. Go ahead and test it.

1 Like

I have already, still does the same thing. It waits for the function to finish before proceeding with the loop

I’m on phone lemme try read whats wrong. Any errors or delays in console?

1 Like

Ooh, good to know. So spawn does actually wait until the next opening to execute, while coroutine.wrap doesn’t.

function spawn(function, ...)
    coroutine.wrap(function)(...)
end
1 Like

No errors related to the issue at hand no.

Yes it’s awful and waits over roblox 30hz old tick system

Move the loop outside of the coroutine. Start the coroutine inside of the loop.

Your issue is that your loop is inside of one coroutine, but it still waits for the code inside.

If you put the code inside into a coroutine, then the loop will simply set it off and not wait for it to finish. Then it will do so again the next iteration.

while wait(.1) do
coroutine.wrap(function()
	for i, v in pairs(items:GetChildren()) do
		if not v.PrimaryPart:FindFirstChild("IsGrabbed") and not v:FindFirstChild("WeldTo") then
		if (RespawnInfo[v] - v.PrimaryPart.Position).Magnitude >= respawndist then
			TimerInfo[v] = TimerInfo[v] + 0.1
		end
		
		if TimerInfo[v] >= respawntime then
			Respawn(v, RespawnInfo[v])
			TimerInfo[v] = 0
		end
		else
		TimerInfo[v] = 0
		end
	end
end)()
end

Again, be careful because concurrency is complicated to get right.

12 Likes

That fixed it! Thank you! (30 characters)

Thank you too @b_oolean for the help and contribution!

1 Like

You’re welcome, stay safe :>

thirteeeeee characters

1 Like

C-C-C-Couroutinee saved the day :sunglasses::sunglasses:

3 Likes