• Print

Author Topic: Help with Check AFK command?  (Read 23824 times)

0 Members and 1 Guest are viewing this topic.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Help with Check AFK command?
« Reply #45 on: May 18, 2016, 08:03:57 pm »
How can I make the clientside timer.Stop stop a shared timer.Create?
I'll answer with a question, and hopefully, you'll go 'DOH!'
What ways can you have a client call a server function?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #46 on: May 18, 2016, 08:08:11 pm »

I'll answer with a question, and hopefully, you'll go 'DOH!'
What ways can you have a client call a server function?

Well I get that, that's what I used with the clientRPC, but that's not what I'm trying to do here (I think) I'm trying to make the timer in the shared file stop when executed in the client... can it be used like that? Like, can a clientRPC be used to stop a timer like that? I thought it could only be used to execute a function.
« Last Edit: May 19, 2016, 08:59:27 am by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #47 on: May 19, 2016, 08:30:29 am »
I'm not sure what JamminR is thinking of, but I think the net library could be used for this. I've never used it before though, and it's pretty difficult from what I've seen, so good luck.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #48 on: May 19, 2016, 02:51:56 pm »
I've used the net library before. Smart idea. However, I'm trying to use it in this code:
Code: Lua
  1. -- shared
  2. local CATEGORY_NAME = "Utility"
  3.  
  4. util.AddNetworkString( "Stop Timer" )
  5.  
  6. function forceSpec( target_ply )
  7.  
  8.         message = "You have been forced to AFK for not responding to the AFK check."
  9.        
  10.         target_ply:ConCommand( "ttt_spectator_mode 1" )
  11.         ULib.tsayError( target_ply, message )
  12.  
  13. end
  14.  
  15. function ulx.cafk( calling_ply, target_ply )
  16.  
  17.         ULib.clientRPC( target_ply, "ulx.cafkClientSide" )
  18.         ulx.fancyLogAdmin( calling_ply, "#A is checking if #T is AFK.", target_ply )
  19.  
  20.         timer.Create( "AFK Timer", 10, 1, function() forceSpec( target_ply ) end )
  21.  
  22. end
  23.  
  24.  
  25.  
  26. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  27. cafk:addParam{ type=ULib.cmds.PlayerArg }
  28. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  29. cafk:help( "Used to check if a player is AFK." )
  30.  
  31.  
  32.  
  33. net.Receive( "Stop Timer", function()
  34.  
  35.         timer.Remove( "AFK Timer" )
  36.  
  37. end )
  38.  
  39. ---------------------------------------------------------------------------------------------------------------- client
  40.  
  41. -- cut out most derma code
  42.         afkButton.DoClick = function()
  43.  
  44.                 RunConsoleCommand( "say", "I'm not AFK." )
  45.                 afkBox:Close()
  46.                 net.Start( "Stop Timer" )
  47.                         net.WriteString( "Stop the timer" )
  48.                 net.SendToServer()
  49.  
  50.         end
  51.  
  52. end

And it's throwing the error:
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:3: attempt to call field 'AddNetworkString' (a nil value)
  1. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:3
and I don't know why, isn't util.AddNetworkString a thing that can be manually defined? (which I did with "Stop Timer")

AHA! I fixed it! Because I was making it in a shared file, I had to put in a
Code: Lua
  1. if SERVER then
to define it, and now it's working perfectly!

shared.lua=
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2. if SERVER then
  3.  
  4.         util.AddNetworkString( "Stop Timer" )
  5.  
  6. end
  7.  
  8. function forceSpec( target_ply )
  9.  
  10.         message = "You have been forced to AFK for not responding to the AFK check."
  11.        
  12.         target_ply:ConCommand( "ttt_spectator_mode 1" )
  13.         ULib.tsayError( target_ply, message )
  14.  
  15. end
  16.  
  17. function ulx.cafk( calling_ply, target_ply )
  18.  
  19.         ULib.clientRPC( target_ply, "ulx.cafkClientSide" )
  20.         ulx.fancyLogAdmin( calling_ply, "#A is checking if #T is AFK.", target_ply )
  21.  
  22.         timer.Create( "AFK Timer", 10, 1, function() forceSpec( target_ply ) end )
  23.  
  24. end
  25.  
  26.  
  27.  
  28. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  29. cafk:addParam{ type=ULib.cmds.PlayerArg }
  30. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  31. cafk:help( "Used to check if a player is AFK." )
  32.  
  33.  
  34.  
  35. net.Receive( "Stop Timer", function()
  36.  
  37.         timer.Remove( "AFK Timer" )
  38.  
  39. end )

client.lua =
Code: Lua
  1. surface.CreateFont( "AFKText", {
  2.         font = "DebugFixed", -- Use the font-name which is shown to you by your operating system Font Viewer, not the file name
  3.         extended = false,
  4.         size = 14,
  5.         weight = 500,
  6.         blursize = 0,
  7.         scanlines = 0,
  8.         antialias = true,
  9.         underline = false,
  10.         italic = false,
  11.         strikeout = false,
  12.         symbol = false,
  13.         rotary = false,
  14.         shadow = false,
  15.         additive = false,
  16.         outline = false,
  17. } )
  18.  
  19. function ulx.cafkClientSide()
  20.        
  21.         local afkBox = vgui.Create( "DFrame" )
  22.         afkBox:MakePopup()
  23.         afkBox:SetSize( 600,200 )
  24.         afkBox:Center()
  25.         afkBox:SetTitle( "Are you AFK?" )
  26.         afkBox:SetDraggable( false )
  27.         afkBox.Width = 600
  28.         afkBox.Height = 200
  29.         afkBox:ShowCloseButton( false )
  30.  
  31.  
  32.  
  33.  
  34.         local afkText = vgui.Create( "DLabel", afkBox )
  35.         afkText:SetText( "Are You AFK?" )
  36.         afkText:Center()
  37.         afkText:SetWidth( 600 )
  38.         afkText:SetFont( "AFKText" )
  39.  
  40.  
  41.  
  42.         local afkButton = vgui.Create( "DButton", afkBox )
  43.         afkButton:SetPos( afkBox.Width/2-25, afkBox.Height/2+30 )
  44.         afkButton:SetText( "Not AFK" )
  45.         afkButton.DoClick = function()
  46.  
  47.                 RunConsoleCommand( "say", "I'm not AFK." )
  48.                 afkBox:Close()
  49.                 net.Start( "Stop Timer" )
  50.                         net.WriteString( "Stop the timer" )
  51.                 net.SendToServer()
  52.  
  53.         end
  54.  
  55. end

No errors, and everything is working great! Thanks for all the help guys!
« Last Edit: May 19, 2016, 03:01:35 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Help with Check AFK command?
« Reply #49 on: May 19, 2016, 03:23:34 pm »
However you want to send the data, clientrpc, server side only console command, net data, simply put the timer stop in a function on the server, have the client execute/call it.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #50 on: May 23, 2016, 04:20:39 am »
Yeah after testing it frequently it works perfectly. Thanks or all the help guys!
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print