Hello!
I’m currently working on a system where I create a coroutine that surrounds a while-loop that calls another coroutine. I’m not having trouble getting it to work, I’m just wondering if it’s a bad habit or just useless to be nesting coroutines. Here’s an example:
local findingTargets = coroutine.create(function()
while wait(1) do
MobModule.findPlayersInRange(Tables, MobModel, FollowRange)
-- More code goes here
end
end)
coroutine.resume(findingTargets)
The modulescript code is along these lines (P.S. I’m also wonder if it’s a bad habit to surround a coroutine.wrap() in a pcall):
findPlayersInRange = function(Tables, MobModel, Range)
local findPlayersInRangeWrap = coroutine.wrap(function()
--code goes here
end)
local success, errormessage = pcall(function() findPlayersInRangeWrap() end)
if(not success)then print(errormessage) end
end,
The reason I’m doing this is because I need the while-loop to be in it’s own thread so that I can have other functions running on the same script as the while-loop. I guess the real question is if I should get rid of the coroutine.wrap() and just have the code in the modulescript. Thanks!
Edit: Shout out to Pokemoncraft5290 for fixing my code formatting!
local findingTargets = coroutine.create(function()
while wait(1) do
MobModule.findPlayersInRange(Tables, MobModel, FollowRange)
-- More code goes here
end
end)
coroutine.resume(findingTargets)
The modulescript code is along these lines (P.S. I’m also wonder if it’s a bad habit to surround a coroutine.wrap() in a pcall):
findPlayersInRange = function(Tables, MobModel, Range)
local findPlayersInRangeWrap = coroutine.wrap(function()
--code goes here
end)
local success, errormessage = pcall(function() findPlayersInRangeWrap() end)
if(not success)then print(errormessage) end
end,
It’s not malicious, the loops themselves create this error (“script timeout”) when it has no timeout, and if it really shouldn’t be used they would have removed it, it’s just an option.
Either way, it would be more useful to use Heartbeat.