I reccomend this way:
local time
hook.Add( "HUDPaint", "CurTimeDelay", function()
if not time then time = CurTime() end
surface.SetFont( "DermaDefault" )
surface.SetTextColor( 255, 255, 255, 255 )
surface.SetTextPos( 100, 100 )
local percent = math.Clamp( ( CurTime() - time ) / 5 * 100, 0, 100 )
surface.DrawText( percent .. "%" )
end )
It doesn’t do anything dodgy like adding 1 onto a number every frame (which is obviously dependant on the user’s hardware speed), and will always give accurate results.
It also doesn’t use any unnecessary timers, it just uses the way more simple and way more logical method of using a single variable.
Whenever you want the timer to restart, you simply need to run:
time = CurTime()
[editline]20th August 2017[/editline]
If you want this to work for a DLabel (which I don’t reccomend because of the frequent updating), then it’s just a matter of doing the exact same thing:
local PercentText = vgui.Create( "DLabel" )
PercentText:SetPos( 100, 100 )
PercentText:SetText( "0%" )
hook.Add( "HUDPaint", "CurTimeDelay", function()
if not PercentText.Time then PercentText.Time = CurTime() end
local percent = math.Clamp( ( CurTime() - PercentText.Time ) / 5 * 100, 0, 100 )
PercentText:SetText( percent .. "%" )
end )