• Displaying Timer on HUD
    4 replies, posted
Hello, I am trying to make a timer, from my addon, display on my darkrp hud. The timer code is: [CODE]if ( text == "/squidditch" ) and ply:IsAdmin() then timer.Create("ClassTime", 500, 1, function() end )[/CODE] Then, in my HUD, I inserted the code [CODE]local LineOne = "Class Time Left:" local LineTwo = math.Round( timer.TimeLeft("ClassTime") ) surface.SetFont("Parry Hotter") --Font must be set before getting the size, or the size will come out wrong. local w, h = surface.GetTextSize(LineOne) --Line one dimensions. local ww, hh = surface.GetTextSize(LineTwo) --Line two dimensions. local LongestLine = math.max(w, ww) local Padding = 4 local Spacing = 2 hook.Add("HUDPaint", "My text box script.", function() local BoxWidth = LongestLine+(Padding*2) local BoxHeight = (h+hh)+(Padding*2) draw.RoundedBox(4, 0, 0, BoxWidth, BoxHeight, color_black) draw.SimpleText(LineOne, "default", BoxWidth*0.5, Padding+(h*0.5), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) draw.SimpleText(LineTwo, "default", BoxWidth*0.5, Padding+h+Spacing+(hh*0.5), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) end)[/CODE] In my console, I get the error [CODE][ERROR] lua/includes/extensions/math.lua:160: attempt to perform arithmetic on local 'num' (a nil value) 1. Round - lua/includes/extensions/math.lua:160 2. DrawInfo - addons/ethan's class system/lua/darkrp_modules/hud/cl_hud.lua:120 3. DrawHUD - addons/ethan's class system/lua/darkrp_modules/hud/cl_hud.lua:275 4. unknown - addons/ethan's class system/lua/darkrp_modules/hud/cl_hud.lua:434[/CODE] [editline]22nd April 2017[/editline] I got it working on the HUD, but now I need to know how to make the timer reset when /squidditch is executed.
[code]if text == "/squidditch" then if timer.Exists("ClassTime") then timer.Remove("ClassTime") end timer.Create("ClassTime", 500, 1, function() end ) end[/code] also instead of doing this dumb timer stuff just use CurTime() e.g. [code]if text == "/squidditch" then startTime = CurTime() end[/code] [code]local LineTwo = math.min(0, math.Round((startTime + 500) - CurTime()))[/code]
I put that in, but now when i do /squidditch, the time always shows up as 0 on the hud. [editline]22nd April 2017[/editline] Ok so the timer counts down from 500 when server starts up, and it displays on the hud. but when the timer resets when i execute the command, the HUD doesnt update with the new time, any fixes?
Where are you defining the text var? Are you checking for ply messages on the server then sending a net message or is it all on the client?
It's all serverside. Code in HUD: [code] local LineOne = "Time Until Next Class:" local LineTwo = math.Round( timer.TimeLeft("ClassTime") ) surface.SetFont("DarkRPHUD2") --Font must be set before getting the size, or the size will come out wrong. local w, h = surface.GetTextSize(LineOne) --Line one dimensions. local ww, hh = surface.GetTextSize(LineTwo) --Line two dimensions. local LongestLine = math.max(w, ww) local Padding = 4 local Spacing = 2 hook.Add("HUDPaint", "My text box script.", function() local BoxWidth = LongestLine+(Padding*2) local BoxHeight = (h+hh)+(Padding*2) draw.RoundedBox(1, 0, 0, BoxWidth, BoxHeight, color_black) draw.SimpleText(LineOne, "default", BoxWidth*0.5, Padding+(h*0.5), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) draw.SimpleText(LineTwo, "default", BoxWidth*0.5, Padding+h+Spacing+(hh*0.5), color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) end)[/code] In my addon: [code]timer.Create("ClassTime", 500, 0, function() end ) hook.Add( "PlayerSay", "EthanClass", function( ply, text, public ) text = string.lower( text ) -- Make the chat message entirely lowercase if ( text == "/startclass1" ) and ply:IsAdmin() then if timer.Exists("ClassTime") then timer.Remove("ClassTime") end timer.Create("ClassTime", 500, 1, function() RunConsoleCommand( "DarkRP", "Class" ) end ) house1 = { "Hufflepuff" } house2 = { "Gryffindor" } house3 = { "Slytherin" } house4 = { "Ravenclaw" } class3 = { "Wizardry", "Charms", "History of Magic" } class3half = { "Muggle Studies", "DADA", "Transfiguration" } class4 = { "Class 1", "Class 3" } class5 = { "Class 2", "Class 4" } RunConsoleCommand( "DarkRP", "Class", house1[math.random( 1, #house1 )], " + ", house4[math.random( 1, #house4 )], class3[math.random( 1, #class3 )], class4[math.random( 1, #class4 )], " | ", house2[math.random( 1, #house2 )], " + ", house3[math.random( 1, #house3 )], class3half[math.random( 1, #class3half )], class5[math.random( 1, #class5 )] ) RunConsoleCommand( "ulx", "playsound", "school_bell_1.mp3" ) timer.Create("NotifyTime", 50, 8, function() RunConsoleCommand( "ulx", "csay", math.Round( timer.TimeLeft("ClassTime") ) ) end ) end end )[/code] The issue is that when i type /startclass1, the time on the box in my HUD doesn't update, it keeps counting down from original timer!
Sorry, you need to Log In to post a reply to this thread.