Task.delay or os.clock for cooldowns in an entity

I have an Object-Oriented Entity module which has states that have cooldowns.

I currently just check every single tick, if the state is enabled, and if its been longer than said cooldown to disable that state.

It looks like this,

if titan.disabledStates.arms[1] then
	--true
	titan:StopAnimation(titan.animations["LeftGrab"])
	if os.clock() - titan.disabledLogs.arms[1] > titan.armDisableTime then
		titan.disabledStates.arms[1] = false
	end

	titan.leftLowerArm.Transparency = 1
	titan.leftHand.Transparency = 1

	if titan.grabbedSide == "Left" then
		titan:StopGrab()
	end
else
	--false
	titan.leftLowerArm.Transparency = 0
	titan.leftHand.Transparency = 0
end

What i want to know is, would it be more performant to just have a seperate thread and do task.delay to just disable the state?

1 Like

os.clock() may be the most accurate measurement, but the performance is not great, especially in loops. Try using tick() instead if you’re looking to compare time deltas, and use task.delay or task.wait to actually wait for them instead of checking them every frame to wait, which wastes resources.

task.wait() actually returns the deltaTime that it waited for, so you could actually use that instead of RunService.HeartBeat:Wait(), for example;

local totalTime = 0
for i = 1, 100 do
    totalTime += task.wait(8) --wait for approximately 8 seconds, to wait for a frame just like RunService.HeartBeat, call task.wait with no input
    if totalTime > 60 then
        print("threshold reached")
        totalTime = 0
    end
end
2 Likes

Using task.delay would be more efficient due to preventing excessive computations & only runs when needed. Just call task.delay(titan.armDisableTime, function() titan.disabledStates.arms[1] = false end) for better performance.

2 Likes