Lua is not multithreaded -- all coroutines run on the same thread. Thus busy-waiting loops like you've written block everything until it's done.
A coroutine describes the place it's executing at (and all the local variables). resume and yield switches between which coroutine is being actively used by the interpreter. But this is a switch -- only one can be active at a time.
Due to careful scheduling, it looks like many coroutines are happening at the same time. What is actually happening is that things like wait make the current coroutine yield; after the current ROBLOX-managed-coroutine yields, ROBLOX looks for the next coroutine that needs to be started (e.g., because an event fired, or because the duration of a wait ended), and resumes that one.
I highly recommend not putting more than one statement on a line, and putting spaces around operators (e.g., + or =).
You can do some code cleanup in your script.
if not tim then tim=0 end can be replaced with tim = tim or 0.
- This can be combined with the previous to just make
tim = tonumber(tim) or 0, though I'm not really sure why you want to accept non-numbers as the first parameter.
- It makes even less sense to coerce
f to a function if it's not.
- Don't define your function inside the print! That's completely unreadable and unnecessary.
- Same trick can be applied to
str. Except that tostring can't return nil, so that shouldn't happen.
At this point you have something like this:
local function doNothing()
end
local CustomDelay = coroutine.wrap(function(tim,f)
tim = tonumber(tim) or 0
assert(type(f) == "function")
local supposedtime = os.time() + tim
local function functionHex()
return tostring(f):sub(10)
end
print('Delaying function "' .. functionHex() .. '" (' .. tostring(tim) .. ') seconds.')
repeat
doNothing()
until os.time() >= supposedtime
local s,e = pcall(f)
if not s then
warn('Error while delaying function: ' .. e)
end
return s,e
end)
CustomDelay(3,function()
print("Woo!")
end)
warn("This should come before the 'Woo!', since it is not delayed")
functionHex really doesn't need to pull the local f out. You should just make it an argument and move it outside of CustomDelay.
The result is much cleaner:
local function doNothing()
end
local function functionHex(f)
return tostring(f):sub(10)
end
local CustomDelay = coroutine.wrap(function(tim,f)
tim = tonumber(tim) or 0
assert(type(f) == "function")
local supposedtime = os.time() + tim
print('Delaying function "' .. functionHex(f) .. '" (' .. tostring(tim) .. ') seconds.')
repeat
doNothing()
until os.time() >= supposedtime
local s, e = pcall(f)
if not s then
warn('Error while delaying function: ' .. e)
end
return s, e
end)
CustomDelay(3,function()
print("Woo!")
end)
warn("This should come before the 'Woo!', since it is not delayed")
1 comment
Alright, thanks! I didn't know much about coroutines, and really didn't care about cleaning my code, but perhaps I should start doing so.