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) afterdelay 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
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.
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?
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.