So for the longest time I’ve been using spawn(function() end) and delay(time, function() end) to accomplish running yielding code, without pausing the current operation. I’m aware that this schedules the code within to a future thread. I’m always aware of coroutines, I use those sometimes too.
The problem is, if I call any of those 3 methods too often (or on hardware that’s REALLY bad) they get really delayed. It seems like their operations perform at the proper speed, it’s just really delayed. So for example:
local calltime = os.clock()
delay(5,function()
print(os.clock()-calltime)
end)
Ideally that would output anywhere from 4.984… to 5.016…, but when it gets really bad it can print anywhere from 5 to 25. I’ve tested this with a variety of things including spawns, coroutines, and even the tweenservice (the tweenservice seems to have the same issues).
So instead I’ve decided to try out a custom method for doing this:
local RSTime = 0.01667
local MultiActions = {}
function New(Yield,Func)
if not Func then -- I'm doing this so I can just 'replace all' on spawn and delay
Func = Yield
Yield = RSTime
end
local Execute = os.clock()+Yield
local ID = GlobalAPI:UniqueIdentifier("Clock")
if MultiActions[ID] ~= nil then ID = GlobalAPI:UniqueIdentifier(ID) end
MultiActions[ID] = {Time = Execute,f = Func}
end
local RS = game:GetService('RunService')
RS.Heartbeat:connect(function(step)
RSTime = (RSTime+step)/2
for i,v in pairs(MultiActions) do
if os.clock()-v.Time >= (step/2) then
MultiActions[i] = nil
v.f()
end
end
end)
It seems to work perfectly fine, but I have yet to stress test it. Does anyone have any experience with this type of method themselves? Anything I should look out for? Is this just a bad idea in general?
BTW my globalAPI:UniqueIdentifier() function just returns a unique id string beginning with anything that’s passed.
I’m not asking for a better alternative, I have an alternative that I posted about. I’m asking if anyone has tried anything similar and if they know if it really is better or not.
I’m sorry but I just tried the same code and it was always within the 5.00 - 5.02. I’m not sure what the problem with yours might be but you may just be overworking the system.
Well yes, I am overworking the system. The problem is, everything else runs perfectly except for anything wrapped in spawn, delay, or a coroutine. I should point out that I’m using a custom wait function based on heartbeat as well, since I noticed wait() had this exact same problem.
I think it has to do with how roblox schedules things, I’m not sure.
You might say “well just don’t overwork the system” however I’ve seen these exact problems happen in normal gameplay for users on bad hardware. So it’s important that I get it working even when everything is all laggy.
Ok, I added a while loop within another script that kept producing while loops and got 13 seconds. The thing is, I ran through those loops 5000 times within those 13 seconds, so my best advice would be to go through your script and try moderate the amount you’re working the system. Theres no reason you should be pushing the system to that extent. There are also probably shortcuts you could be able to find help bypass these.
I just said… that normal gameplay experiences these problems for people with bad hardware.
Obviously in normal conditions, I’m not calling them 5000 times. That would be ridiculous. We have to call them 5000 times to get OUR systems to get the lag related issues. For other users, especially mobile users, they only have to call it say 10 to 15 times to get issues.