Wrap it with a c-c-c-couroutinee
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.
Wrap it with a c-c-c-couroutinee
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.
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.
@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.
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.
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.
@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.
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?
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.
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?
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
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.
That fixed it! Thank you! (30 characters)
Thank you too @b_oolean for the help and contribution!
You’re welcome, stay safe :>
thirteeeeee characters
C-C-C-Couroutinee saved the day
You don’t have to unless you start involving the coroutines in other matters such as using running and yield. Coroutines work in virtually the same mannerism as spawn/delay and should be used the latter two. Those aren’t ideal functions especially in a run time intensive environment where the guarantee of functions running is critical.
You can read up on various horror stories and the comparison between each in this thread (which was previously linked by someone else):
Thanks man! I just had this same issue and never even knew about coroutines. You really do learn something new every day!