• Print

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

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Help with Check AFK command?
« on: May 09, 2016, 09:39:53 pm »
So I'm trying to make this code for a Check AFK command that I've seen in a couple other servers that may be very useful for a TTT server I'm staff on. Currently I have the code:
Code: Lua
  1. ------------------------------ Check AFK ------------------------------
  2.         function ulx.cafk()
  3.                 local afkBox = vgui.Create("DFrame")
  4.                 afkBox:MakePopup()
  5.                 afkBox:SetSize(600,200)
  6.                 afkBox:SetPos(ScrW()/2, ScrH()/2)
  7.                 afkBox:SetTitle("Are you AFK?")
  8.                 afkBox:SetDraggable( true )
  9.                 afkBox.Width = 600
  10.                 afkBox.Height = 200
  11.  
  12.  
  13.  
  14.  
  15.                 local afkText = vgui.Create("DLabel", afkBox)
  16.                 afkText:SetText("Are You AFK?")
  17.                 afkText:SetPos(afkBox.Width/2-18, afkBox.Height/2)
  18.                 afkText:SetWidth(600)
  19.  
  20.  
  21.  
  22.                 local afkButton = vgui.Create("DButton", afkBox)
  23.                 afkButton:SetPos(afkBox.Width/2, afkBox.Height/2+40)
  24.                 afkButton:SetText("Not AFK")
  25.                 afkButton.DoClick = function()
  26.                         RunConsoleCommand("ulx asay", "I'm not AFK.") -- Gets the error: "RunConsoleCommand: Command has invalid characters! (ulx asay (' '))
  27.         --The first parameter of this function should contain only the command, the second parameter should contain arguments.
  28.                         afkBox:Close()                 
  29.                 end
  30.         end
  31. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  32. cafk:defaultAccess( ULib.ACCESS_ADMIN )
  33. cafk:help( "Checks if a player is currently AFK." )

It was working fine when I didn't have it in the actual util.lua from ULX, but when I had it as a normal function in a random lua file. When I put it in and tried to run !cafk I got the error
Code: [Select]
addons/ulx/lua/ulx/modules/sh/util.lua:4: attempt to index global 'vgui' (a nil value)
What I assumed is that I couldn't use vgui.Create (or vgui in general) because it's a clientside command and not serverside, but I didn't know any other way to do it.


Basically, my goal is this: Bring up a screen with the title "Are you AFK?" with text also saying "Are you AFK?" and a button just below it saying "Not AFK." I also want to add a timer along with it so that if the button isn't pressed after a certain amount of time, they will be automatically set to spectator. This is for TTT by the way.


Note: Some of you may notice I made another post like this recently, but I was asked to see if I could make something like this for the server to lessen chat flood and to streamline the process.

Cheers!


Also, I just realized that since the Derma is made locally, that means it can't be used on other clients, as it is a clientside function, correct? If that's the case, how can I make something like this show up?
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 #1 on: May 10, 2016, 04:50:00 am »
What I assumed is that I couldn't use vgui.Create (or vgui in general) because it's a clientside command and not serverside, but I didn't know any other way to do it.

If you check out the wiki page for vgui.Create, you'll see that it's the other way around (as is the entire VGUI library): VGUI can only be used on the client.

Now, the question comes down to where you are placing this file. Last time I messed around with ULX and clientside effects I remember I had to have two (maybe more) files.

Pretty sure you need one file in <youraddonname>/lua/ulx/modules/cl/, and this is where you put all your clientside code (such as VGUI). Just create a function preceeded by "ulx." to put it in the ulx table so you can reference it elsewhere in ULX files. Then you'll want to get another file in <youraddonname>/lua/ulx/modules/sh/ and this is where you can create your actual command.

Also, I just realized that since the Derma is made locally, that means it can't be used on other clients, as it is a clientside function, correct? If that's the case, how can I make something like this show up?

Yes, you're going to need to send the lua to the clients that you want to run this on somehow. I used ULib.clientRPC(), and that will probably work for your use case as well.

Basically just move your ulx.cafk() function to <youraddonname>/lua/ulx/modules/cl/ (make sure to remove the bits to turn it into a command) and then create a command (in <youraddonname>/lua/ulx/modules/sh/) that runs ULib.clientRPC with your function and function parameters, and you should be good to go.

Good luck!

P.S. if you're confused about the <youraddonname>, that's because you shouldn't be editing core ULX or ULib files (it will make updating a pain). Instead, create your own addon and copy the structure of the ulx addon folder and just put all your modifications in there.
« Last Edit: May 10, 2016, 04:52:33 am by roastchicken »
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 #2 on: May 10, 2016, 07:51:58 am »
Alright thanks for the help. I'm not at home right now but I'll test it out when I get home  :)

By the way; I do have it working but only when I had it as a solo function and when I open script on it it will work and show it, but it will be slightly displaced (as in, not centered) and sometimes the RunConsoleCommand doesn't work (when I had it on "say" instead of "ulx asay". How can I make it so that it will execute ulx asay instead of say? 


Cheers!
« Last Edit: May 10, 2016, 07:54:34 am by Masterbinkie »
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 #3 on: May 10, 2016, 12:07:20 pm »
I'm not sure what trouble you're having with it running ulx asay instead of say; just change the command you supply to RunConsoleCommand.
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 #4 on: May 10, 2016, 12:29:17 pm »
I'm not sure what trouble you're having with it running ulx asay instead of say; just change the command you supply to RunConsoleCommand.

I mentioned it in my code. Basically when I had
Code: Lua
  1. RunConsoleCommand("ulx asay", "I'm not AFK.")
it gives me the error:
Code: [Select]
"RunConsoleCommand: Command has invalid characters! (ulx asay (' '))
        --The first parameter of this function should contain only the command, the second parameter should contain arguments."

That's the issue I'm having. It's saying that the first param should only have the command, and I think it's confusing "ulx" and "asay" as two different variables accidentally put into the first variable. Do you know why this is?

Basically just move your ulx.cafk() function to <youraddonname>/lua/ulx/modules/cl/ (make sure to remove the bits to turn it into a command) and then create a command (in <youraddonname>/lua/ulx/modules/sh/) that runs ULib.clientRPC with your function and function parameters, and you should be good to go.
Also, is what you mean here just putting everything in the code I have above (except the whole ulx stuff at the bottom) into the client file? I have CustomCommands\lua\ulx\modules\sh\sh_cc_cafk.lua and a CustomCommands\lua\ulx\modules\cl\cl_cc_cafk.lua

Should I move all that code into the cl_cc_cafk.lua and then in the sh use the ULib.clientRPC() command to simulate a clientside command activated on the serverside?

I'm a bit confused so sorry if this sounds confusing :P

Also, another question, which file do I put the ULib.clientRPC() util in?
« Last Edit: May 10, 2016, 12:44:29 pm by Masterbinkie »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #5 on: May 10, 2016, 12:36:37 pm »
I now have in the cl_cc_cafk.lua file this code:
Code: Lua
  1.         function ulx.cafk()
  2.        
  3.                 local afkBox = vgui.Create("DFrame")
  4.                 afkBox:MakePopup()
  5.                 afkBox:SetSize(600,200)
  6.                 afkBox:SetPos(ScrW()/2, ScrH()/2)
  7.                 afkBox:SetTitle("Are you AFK?")
  8.                 afkBox:SetDraggable( true )
  9.                 afkBox.Width = 600
  10.                 afkBox.Height = 200
  11.  
  12.  
  13.  
  14.  
  15.                 local afkText = vgui.Create("DLabel", afkBox)
  16.                 afkText:SetText("Are You AFK?")
  17.                 afkText:SetPos(afkBox.Width/2-18, afkBox.Height/2)
  18.                 afkText:SetWidth(600)
  19.  
  20.  
  21.  
  22.                 local afkButton = vgui.Create("DButton", afkBox)
  23.                 afkButton:SetPos(afkBox.Width/2, afkBox.Height/2+40)
  24.                 afkButton:SetText("Not AFK")
  25.                 afkButton.DoClick = function()
  26.                         RunConsoleCommand("ulx asay", "I'm not AFK.")
  27.                         afkBox:Close()                 
  28.                 end
  29.         end

What do I then put in the sh file? Do I need to AddCSLua?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #6 on: May 10, 2016, 01:06:58 pm »
Ok I just tried something else. I'm getting the error:
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/cl/cl_cc_cafk.lua:2: function arguments expected near '.'
  1. unknown - addons/customcommands/lua/ulx/modules/cl/cl_cc_cafk.lua:0

when I'm trying to use this code:
Code: Lua
  1. local function forceSpec(target_ply)
  2.         if LocalPlayer():team.GetName( player.GetByID( target_ply ):Team() ) ) == "Spectators" then
  3.                 return
  4.         else
  5.                 LocalPlayer():SetTeam(1002)
  6.         end
  7.  
  8.  
  9. end

I feel like I'm doing something wrong with the LocalPlayer() but I don't know.

Nevermind, found a much easier way to do it. TTT has it's own set of variables for spectator, so I used the following code and it worked:
Code: [Select]
local function forceSpec( calling_ply, target_plys )
for k,v in pairs(player.GetAll()) do
v:ConCommand("ttt_spectator_mode 1")
v:ConCommand("ttt_cl_idlepopup")
end
end
forceSpec()

Of course, now I need to make it into a command so that it isn't activated instantly, and so that it doesn't do it to everyone, and so that it only activates when called upon... what I want it to do is have a timer after the !cafk command appears, so that if the timer gets to a certain duration before the button is clicked, they are put into spectator.
« Last Edit: May 10, 2016, 01:26:24 pm by Masterbinkie »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Help with Check AFK command?
« Reply #7 on: May 10, 2016, 01:38:04 pm »
Ah man I feel like I'm the only one posting here now. :/

Having an issue though, I understand what it means but I'm not entirely sure how to fix it.

Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:11: bad argument #2 to ULib.cmds.TranslateCommand (function expected)

Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. local function forceSpec( calling_ply, target_ply )
  4.         for k,v in pairs(target_ply) do
  5.                 v:ConCommand("ttt_spectator_mode 1")
  6.                 v:ConCommand("ttt_cl_idlepopup")
  7.  
  8.                 ulx.fancyLogAdmin("#T failed to respond to the AFK Check.", target_ply )
  9.         end
  10. end
  11. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  12. cafk:addParam{ type=ULib.cmds.PlayersArg, ULib.cmds.optional }
  13. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  14. cafk:help( "Checks if a player is AFK." )

I know what the error means. The second argument in ulx.command should be the function name of the command, but there is no function command in that file since it's in the cl file as it uses vgui which is clientside only. What do I need to put there to have it reference to the client function that I have (
Code: Lua
  1. function ulx.cafk()
  2.        
  3.         local afkBox = vgui.Create("DFrame")
  4.         afkBox:MakePopup()
  5.         afkBox:SetSize(600,200)
  6.         afkBox:SetPos(ScrW()/2, ScrH()/2)
  7.         afkBox:SetTitle("Are you AFK?")
  8.         afkBox:SetDraggable( true )
  9.         afkBox.Width = 600
  10.         afkBox.Height = 200
  11.  
  12.  
  13.  
  14.  
  15.         local afkText = vgui.Create("DLabel", afkBox)
  16.         afkText:SetText("Are You AFK?")
  17.         afkText:SetPos(afkBox.Width/2-18, afkBox.Height/2)
  18.         afkText:SetWidth(600)
  19.  
  20.  
  21.  
  22.         local afkButton = vgui.Create("DButton", afkBox)
  23.         afkButton:SetPos(afkBox.Width/2, afkBox.Height/2+40)
  24.         afkButton:SetText("Not AFK")
  25.         afkButton.DoClick = function()
  26.                 RunConsoleCommand("ulx asay", "I'm not AFK.")
  27.                 afkBox:Close()                 
  28.         end
  29. end
  30.  
)
as I can't have that in the shared folder because vgui.Create can't be used via the server.

In other words, you mentioned earlier a ULib.clientRPC() function, I'm confused how to use it.
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 #8 on: May 10, 2016, 03:12:56 pm »
Yes, this is not a large forum so if you make four posts in a two hour period you might get lonely XD

I'm going to respond to your posts one by one. It might be more work in the long run because you might have solved some problems, but I don't think I can keep all of your posts in my head at once :P

Prepare yourselves for a very long post...



I mentioned it in my code. Basically when I had
Code: Lua
  1. RunConsoleCommand("ulx asay", "I'm not AFK.")
it gives me the error:
Code: [Select]
"RunConsoleCommand: Command has invalid characters! (ulx asay (' '))
        --The first parameter of this function should contain only the command, the second parameter should contain arguments."

That's the issue I'm having. It's saying that the first param should only have the command, and I think it's confusing "ulx" and "asay" as two different variables accidentally put into the first variable. Do you know why this is?

In the source engine commands are not allowed to contain spaces. Technically "ulx" is a command, and "asay" is an argument. You want to do RunConsoleCommand( "ulx", "asay", "I'm", "not", "AFK." ).

Also, is what you mean here just putting everything in the code I have above (except the whole ulx stuff at the bottom) into the client file?

Should I move all that code into the cl_cc_cafk.lua and then in the sh use the ULib.clientRPC() command to simulate a clientside command activated on the serverside?

Yes

Also, another question, which file do I put the ULib.clientRPC() util in?

I already answered this in a way, but just to clarify: you want to call ULib.clientRPC() in the shared file (CustomCommands\lua\ulx\modules\sh\sh_cc_cafk.lua).



I now have in the cl_cc_cafk.lua file this code: -snipped code-
What do I then put in the sh file? Do I need to AddCSLua?

You just want to create a ULX command that runs ulx.cafk() via ULib.clientRPC(). You do not need to use AddCSLua, as ULX will automatically include the client files if they're in <addon>/lua/ulx/modules/cl/.



Ok I just tried something else. I'm getting the error: -snipped error-
when I'm trying to use this code: -snipped code-

I feel like I'm doing something wrong with the LocalPlayer() but I don't know.

It's always a good idea to check the wiki page for a function. team.GetName shows us that this function accepts one argument, a teamIndex. You only use Player:function() notation when the function needs a reference to a player. team.GetName() does not. I'm not sure what you were attempting to do with the LocalPlayer():, but if you had removed that I believe it would have worked (or at least fixed that specific error).

Nevermind, found a much easier way to do it. TTT has it's own set of variables for spectator, so I used the following code and it worked: -snipped code-

Of course, now I need to make it into a command so that it isn't activated instantly, and so that it doesn't do it to everyone, and so that it only activates when called upon... what I want it to do is have a timer after the !cafk command appears, so that if the timer gets to a certain duration before the button is clicked, they are put into spectator.

Looping through all the players and running console commands on them is an extremely inefficient way of doing this. I don't know the specifics, as it's been a while since I messed with TTT, but I believe there is a function in TTT to force someone to the spectator team. Check out the TTT website, it might give you some information on how to. If not, hopefully someone else here has experience. You could also go sorting through TTT's code, though it is quite a lot.



Ah man I feel like I'm the only one posting here now. :/

Never fear, roast is here! ;D

Having an issue though, I understand what it means but I'm not entirely sure how to fix it.

-snipped error-
-snipped code-

I know what the error means. The second argument in ulx.command should be the function name of the command, but there is no function command in that file since it's in the cl file as it uses vgui which is clientside only. What do I need to put there to have it reference to the client function that I have (-snipped code-)
as I can't have that in the shared folder because vgui.Create can't be used via the server.

In other words, you mentioned earlier a ULib.clientRPC() function, I'm confused how to use it.

Okay, so what you want to do is create a new command. To refresh your memory, it's in this format:

Code: Lua
  1. local CATEGORY_NAME = "My Category"
  2.  
  3. local function ulx.mycommand( calling_ply, myArg1, myArg2 )
  4.   --my code to be run when the command is called
  5. end
  6.  
  7. local mycommand = ulx.command( CATEGORY_NAME, "ulx mycommand", ulx.mycommand, "!mycommand" ) --the third argument here must be a shared function, most likely the function you defined above.
  8. mycommand:addParam{ type=ULib.cmds.PlayerArg } --just an example
  9. mycommand:addParam{ type=ULib.cmds.StringArg } --just an example
  10. mycommand:defaultAccess( ULib.ACESS_SUPERADMIN ) --just an example
  11. mycommand:help( "My help text!" )
  12.  

You're trying to mix in the client-side function with the command registration. You don't want to do that. All you want is a simple command with a body that runs ULib.clientRPC().

In your case, your body would probably just look like this:

Code: Lua
  1. ULib.clientRPC( target_ply, "ulx.cafk" )

I recommend renaming your client-side function to something more descriptive, like "ulx.afkMenu". That way your actual command can be called ulx.cafk. Basically just remove the body of your original code and slap a ULib.clientRPC in there:

Code: Lua
  1. ------------------------------ Check AFK ------------------------------
  2. function ulx.cafk()
  3.     --put ULib.clientRPC here
  4. end
  5. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  6. cafk:defaultAccess( ULib.ACCESS_ADMIN )
  7. cafk:help( "Checks if a player is currently AFK." )



Again, apologies for the humongous post. Hope it helped in some way.
« Last Edit: May 10, 2016, 03:45:05 pm by roastchicken »
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 #9 on: May 10, 2016, 06:08:30 pm »
Ok so I feel like a total asshole right now, but since I didn't see a response for a little bit (I was away anyways) I changed the code (quite a bit too) to just ditch the derma idea. I'm really sorry for this, but I think you'd be able to help me out a bit more! (That is, if you don't hate me for changing everything). I simply changed it to a "do this if you're not afk" sort of thing. Right now I have
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. local function forceSpec( calling_ply, target_ply )
  4.         for k,v in pairs(target_ply) do
  5.                 v:ConCommand("ttt_spectator_mode 1")
  6.                 v:ConCommand("ttt_cl_idlepopup")
  7.  
  8.                 ulx.fancyLogAdmin("#T failed to respond to the AFK Check.", target_ply )
  9.         end
  10. end
  11.  
  12.  
  13. function ulx.cafk( calling_ply, target_ply)
  14.         ULib.tsay(target_ply, "If you are not AFK, please press W, A, S, or D to cancel automatic spectating.")
  15.  
  16.         local passedResponse
  17.         for k,v in pairs(target_ply) do
  18.                 if input.IsKeyDown(KEY_W) then
  19.                         passedResponse = true
  20.                         elseif input.IsKeyDown(KEY_A) then
  21.                                 passedResponse = true
  22.                         elseif input.IsKeyDown(KEY_D) then
  23.                                 passedResponse = true
  24.                         elseif input.IsKeyDown(KEY_S) then
  25.                                 passedResponse = true
  26.                 else
  27.                         passedResponse = false
  28.                 end
  29.                 if passedResponse == false then
  30.                         ulx.fancyLogAdmin( "#T failed to respond to the AFK check." )
  31.                         forceSpec()
  32.                 else
  33.                         ulx.fancyLogAdmin( "#T has passed the AFK check." )
  34.                 end
  35.         end
  36. end
  37. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  38. cafk:addParam{ type=ULib.cmds.PlayersArg, ULib.cmds.optional }
  39. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  40. cafk:help( "Checks if a player is AFK." )

And now, I'm not getting any specific errors about the code itself, just these two:

First, when I first load a game:
Code: [Select]
[ERROR] lua/includes/extensions/table.lua:76: attempt to index local 'dest' (a nil value)
  1. Merge - lua/includes/extensions/table.lua:76
   2. getData - addons/ulx/lua/ulx/xgui/server/sv_groups.lua:13
    3. sendDataTable - addons/ulx/lua/ulx/modules/xgui_server.lua:178
     4. unknown - addons/ulx/lua/ulx/modules/xgui_server.lua:98
      5. unknown - addons/ulx/lua/ulx/modules/xgui_server.lua:65
       6. unknown - lua/includes/modules/concommand.lua:54

And secondly, when I actually try to use the command:
Code: [Select]
[ERROR] addons/ulib/lua/ulib/shared/commands.lua:943: bad argument #1 to ULib.tsay (nil,Player,Entity expected, got table)
  1. error - [C]:-1
   2. checkArg - addons/ulib/lua/ulib/shared/misc.lua:585
    3. tsay - addons/ulib/lua/ulib/shared/messages.lua:25
     4. call - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:14
      5. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
       6. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
        7. unknown - lua/includes/modules/concommand.lua:54

I haven't done anything to edit either of these, do you have any idea what the issue is?

(Again, sorry about changing everything!!!!)

EDIT: I may have found my issue by myself. I didn't make ULib.tsay a function. I'll see if I can figure out how to fix it.

EDIT2: I fixed the error earlier I was having by instead putting in
Code: Lua
  1. function ulx.cafk( calling_ply, target_ply )
  2.         local passedResponse
  3.         for k,v in pairs(target_ply) do
  4.                 ULib.tsay( calling_ply, "If you are not AFK, please press W, A, S, or D to cancel automatic spectating." )
  5.                 if input.IsKeyDown(KEY_W) then
  6.                         passedResponse = true
  7.                         elseif input.IsKeyDown(KEY_A) then
  8.                                 passedResponse = true
  9.                         elseif input.IsKeyDown(KEY_D) then
  10.                                 passedResponse = true
  11.                         elseif input.IsKeyDown(KEY_S) then
  12.                                 passedResponse = true
  13.                 else
  14.                         passedResponse = false
  15.                 end
  16.                 if passedResponse == false then
  17.                         ulx.fancyLogAdmin( "#T failed to respond to the AFK check." )
  18.                         forceSpec()
  19.                 else
  20.                         ulx.fancyLogAdmin( "#T has passed the AFK check." )
  21.                 end
  22.         end
  23. end

All I did was change the position of the ULib.tsay function to inside the for k,v in pairs loop and it sent the message, now all I'm getting when I use it is this:
Code: [Select]
] ulx cafk ^

[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:18: attempt to index global 'input' (a nil value)
  1. call - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:18
   2. __fn - addons/ulib/lua/ulib/shared/commands.lua:943
    3. unknown - addons/ulib/lua/ulib/shared/commands.lua:1296
     4. unknown - lua/includes/modules/concommand.lua:54

If you are not AFK, please press W, A, S, or D to cancel automatic spectating.
However I just realized the ULib.tsay is going to the calling_ply and not the target, however when I set it to target_ply I get the same error as before.
« Last Edit: May 10, 2016, 06:38:29 pm by Masterbinkie »
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 #10 on: May 10, 2016, 07:50:16 pm »
target_ply in your function is being passed a table due to your command setup.
PlayersArg= table of players, EVEN if you only specify one name.
If you only want one at a time, use PlayerArg in your command setup.

Also, your key check isn't going to work. It only checks one time and moves on, it doesn't give the player(s) any time to respond and will always say they are AFK unless they happen to be already moving when you run this.

You may want to get ideas from a release here on the forum from MrPresident - AntiAFK.
/index.php/topic,5963.0.html
It's not quite the same, but will use some of the same checks you want to.
« Last Edit: May 10, 2016, 07:53:38 pm by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #11 on: May 10, 2016, 08:01:54 pm »
The way I see it, you should just stick to the derma idea. Looking at your final code, you're still going to have to run it on the client. The input library is client-side only. Not to mention it wouldn't even work if you did put it on the client. (JamminR explained why)

You still have your old derma code, I highly suggest you keep going with that idea. If you're still struggling to understand, I can help you some more.

And no worries, I don't think you're an ass :P
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 #12 on: May 11, 2016, 01:35:16 pm »
The way I see it, you should just stick to the derma idea. Looking at your final code, you're still going to have to run it on the client. The input library is client-side only. Not to mention it wouldn't even work if you did put it on the client. (JamminR explained why)

You still have your old derma code, I highly suggest you keep going with that idea. If you're still struggling to understand, I can help you some more.

And no worries, I don't think you're an ass :P

Yeah I am kind of struggling here. I'm confused how I can make the sh code reference to the client code. You said to use
Code: Lua
  1. ULib.clientRPC( target_ply, "ulx.cafk" )
but how does that fit into the code?

cl_cc_cafk.lua:
Code: Lua
  1. function ulx.cafk()
  2.    
  3.    local afkBox = vgui.Create("DFrame")
  4.    afkBox:MakePopup()
  5.    afkBox:SetSize(600,200)
  6.    afkBox:SetPos(ScrW()/2, ScrH()/2)
  7.    afkBox:SetTitle("Are you AFK?")
  8.    afkBox:SetDraggable( true )
  9.    afkBox.Width = 600
  10.    afkBox.Height = 200
  11.  
  12.  
  13.  
  14.  
  15.    local afkText = vgui.Create("DLabel", afkBox)
  16.    afkText:SetText("Are You AFK?")
  17.    afkText:SetPos(afkBox.Width/2-18, afkBox.Height/2)
  18.    afkText:SetWidth(600)
  19.  
  20.  
  21.  
  22.    local afkButton = vgui.Create("DButton", afkBox)
  23.    afkButton:SetPos(afkBox.Width/2, afkBox.Height/2+40)
  24.    afkButton:SetText("Not AFK")
  25.    afkButton.DoClick = function()
  26.       RunConsoleCommand("say" "I'm not AFK.")
  27.       afkBox:Close()        
  28.    end
  29. end

I still have that, what do I need to put in the sh file to reference that? I see what I have to use (ULib.clientRPC( target_ply, "ulx.cafk" )) but what exactly does that mean? Where do I put it in the sh code?

Also, Jammin said that my key checks wouldn't work, could it be useful if I were to hook.Add("Think" ... ) during the AFK timer so that it's constantly checking for key inputs by the client?

(Honestly I'm just really confused about ULib.clientRPC( target_ply, "ulx.cafk" ). I don't know how to use it or where to put it.


EDIT: So I have this working so far:
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3.  
  4. local function forceSpec( calling_ply, target_ply )
  5.         for k,v in pairs(target_ply) do
  6.                 v:ConCommand("ttt_spectator_mode 1")
  7.                 v:ConCommand("ttt_cl_idlepopup")
  8.  
  9.  
  10.                 ulx.fancyLogAdmin("#T failed to respond to the AFK Check.", target_ply )
  11.         end
  12. end
  13.  
  14.  
  15. function ulx.cafk( calling_ply, target_ply )
  16.         local passedResponse
  17.                 ULib.tsay( target_ply, "#T if you are not AFK, please press W, A, S, or D to cancel automatic spectating." )
  18. end
  19. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  20. cafk:addParam{ type=ULib.cmds.PlayerArg, ULib.cmds.optional }
  21. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  22. cafk:help( "Checks if a player is AFK." )
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
which sends "#T if you are not AFK, please press W, A, S, or D to cancel automatic spectating." I don't know how "#T" works, because it just sends to me #T not the target name...



« Last Edit: May 11, 2016, 01:56:26 pm by Masterbinkie »
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 #13 on: May 11, 2016, 04:16:43 pm »
garrysmod/addons/myaddonname/lua/ulx/modules/cl/myclientside.lua:

Code: Lua
  1. local function ulx.clientSideFunction()
  2.   --put clientside code here
  3. end

garrysmod/addons/myaddonname/lua/ulx/modules/sh/myserverside.lua:

Code: Lua
  1. local CATEGORY_NAME = "My Category"
  2.  
  3. local function ulx.mycommand( calling_ply, target_player )
  4.   ULib.clientRPC( target_player, "ulx.clientSideFunction" )
  5. end
  6.  
  7. local mycommand = ulx.command( CATEGORY_NAME, "ulx mycommand", ulx.mycommand, "!mycommand" )
  8. mycommand:addParam{ type=ULib.cmds.PlayerArg }
  9. mycommand:defaultAccess( ULib.ACESS_SUPERADMIN )
  10. mycommand:help( "Runs clientside code on <player>." )
« Last Edit: May 11, 2016, 04:18:18 pm by roastchicken »
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 #14 on: May 14, 2016, 04:28:57 pm »
garrysmod/addons/myaddonname/lua/ulx/modules/cl/myclientside.lua:

Code: Lua
  1. local function ulx.clientSideFunction()
  2.   --put clientside code here
  3. end

garrysmod/addons/myaddonname/lua/ulx/modules/sh/myserverside.lua:

Code: Lua
  1. local CATEGORY_NAME = "My Category"
  2.  
  3. local function ulx.mycommand( calling_ply, target_player )
  4.   ULib.clientRPC( target_player, "ulx.clientSideFunction" )
  5. end
  6.  
  7. local mycommand = ulx.command( CATEGORY_NAME, "ulx mycommand", ulx.mycommand, "!mycommand" )
  8. mycommand:addParam{ type=ULib.cmds.PlayerArg }
  9. mycommand:defaultAccess( ULib.ACESS_SUPERADMIN )
  10. mycommand:help( "Runs clientside code on <player>." )

Ok, I'm going to go and try this out.

Sorry for not responding earlier, my internet went out and was unable to do anything for several days.


EDIT: Alright, so I tried that and got the error:
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:3: '(' expected near '.'
  1. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:0

but when I looked in the code to find it, I didn't see anything missing? Can someone point it out to me?

Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. local function ulx.cafk( calling_ply, target_player )
  4.   ULib.clientRPC( target_player, "ulx.cafkClientSide" )
  5. end
  6.  
  7. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  8. cafk:addParam{ type=ULib.cmds.PlayerArg }
  9. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  10. cafk:help( "Used to check if a player is AFK." )

I'm also getting this with the clientside:
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/cl/cl_cc_cafk.lua:26: ')' expected near '"I'm not AFK."'
  1. unknown - addons/customcommands/lua/ulx/modules/cl/cl_cc_cafk.lua:0

Code: Lua
  1. local function ulx.cafkClientSide()
  2.        
  3.         local afkBox = vgui.Create("DFrame")
  4.         afkBox:MakePopup()
  5.         afkBox:SetSize(600,200)
  6.         afkBox:SetPos(ScrW()/2, ScrH()/2)
  7.         afkBox:SetTitle("Are you AFK?")
  8.         afkBox:SetDraggable( true )
  9.         afkBox.Width = 600
  10.         afkBox.Height = 200
  11.  
  12.  
  13.  
  14.  
  15.         local afkText = vgui.Create("DLabel", afkBox)
  16.         afkText:SetText("Are You AFK?")
  17.         afkText:SetPos(afkBox.Width/2-18, afkBox.Height/2)
  18.         afkText:SetWidth(600)
  19.  
  20.  
  21.  
  22.         local afkButton = vgui.Create("DButton", afkBox)
  23.         afkButton:SetPos(afkBox.Width/2, afkBox.Height/2+40)
  24.         afkButton:SetText("Not AFK")
  25.         afkButton.DoClick = function()
  26.                 RunConsoleCommand( "say" "I'm not AFK." )
  27.                 afkBox:Close()                 
  28.         end
  29. end

EDIT2: Ok, so I fixed the error from the shared one (I changed it from LOCAL function ulx.cafk to just function ulx.cafk and that seemed to work.) I thought this may be the same issue with the clientside one, so I removed it but to no avail.
« Last Edit: May 14, 2016, 05:44:22 pm by Masterbinkie »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print