Is it good to use repeat in this case?

Hey there! My question is, Is there any other way to do this? It does the work but i feel it can be a little resource consuming.

Edit: I am doing a notification system where if you hold said notification, The timer will stop until unhovered. So for example if the notification has a length of 4 and you hold when the timer is 2, The timer will persist at 2 until the notification is unhovered.

--// While the user hovers the notifications,
--// the timer will stop there and will continue once the mouse leaves.

local LeftTime = data.Length or 5
local Holding = false
local Interval = 0.05

repeat
	if not Holding then
		LeftTime -= Interval
	end
				
	task.wait(Interval)
until LeftTime <= 0

Majority of the code is not here for privacy reasons :tongue:

1 Like

That works, here is different way;

local endTime = tick() + (data.Length or 5)
local paused = false
local pauseStart = 0
local totalPaused = 0

while true do
	if paused then
		pauseStart = pauseStart == 0 and tick() or pauseStart
	else
		if pauseStart > 0 then
			totalPaused += tick() - pauseStart
			pauseStart = 0
		end
	end

	if tick() - totalPaused >= endTime then
		break
	end

	task.wait(0.1)
end

You can just do debounce:

--!strict
--!optimize 2
--// While the user hovers the notifications,
--// the timer will stop there and will continue once the mouse leaves.
local RunService = game:GetService("RunService")
local Heartbeat = RunService.Heartbeat
local WAIT = Heartbeat.Wait
local TimerEnds:number = os.clock() + 30 --30 seconds
while os.clock()<TimerEnds do
--do stuff
WAIT(Heartbeat)
end
--Debounce ended

Don’t get overwhelmed with

local Heartbeat = RunService.Heartbeat
local WAIT = Heartbeat.Wait

Its just me optimizing living hell out of it :skull:

Cool, this is a UI so stepped may be best.

local Stepped = game:GetService("RunService").Stepped
local TimerEnds = os.clock() + 30

while os.clock() < TimerEnds do
	-- updates
	Stepped:Wait()
end

Thanks!!! I was scared of burning someone’s computer with lots of notifications. ( Consider both of your replies as a solution, i cant put more than one D: )

in this case i would use a while loop instead of a repeat

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.