• Print

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

0 Members and 1 Guest are viewing this topic.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Help with Check AFK command?
« Reply #15 on: May 14, 2016, 08:13:01 pm »
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. RunConsoleCommand( "say" "I'm not AFK." )

You're forgetting to separate the arguments with a comma. You should instead have
Code: Lua
  1. RunConsoleCommand( "say", "I'm not AFK." )
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 #16 on: May 14, 2016, 09:01:06 pm »
You're forgetting to separate the arguments with a comma. You should instead have
Code: Lua
  1. RunConsoleCommand( "say", "I'm not AFK." )

Oh.. duh.. I was so caught up in changing around with ulx asay and normal say I guess I forgot to put it back in! I'll try that out tomorrow.
That's probably why I was confused too, since it said ')' expected.
« Last Edit: May 14, 2016, 09:19:53 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 #17 on: May 15, 2016, 09:36:56 am »
Finally! No errors! It's working perfectly now. Now all I have to do is make a timer.. hmm... The only issue I'm having is it's not exactly centered in the screen.. the frame and label are fine, but I want the button to be slightly under the label. The label is centered and the text is centered in the label, how do I find where the button to be to get it to be just under the label?

Oh, and also, how can I make it so that there is no way to close the box via the X button in the top?

Nevermind, it's just ShowCloseButton( false )
« Last Edit: May 15, 2016, 09:45:27 am 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 #18 on: May 15, 2016, 09:46:47 am »
I'm not familiar enough with derma to help you on the button placement without spending more time than I currently have or wish, but, for the hide close button,
DFrame:ShowCloseButton - this, I think.
"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 #19 on: May 15, 2016, 10:23:20 am »
Ok, now I need help with a timer for it. I had this before:
Code: Lua
  1. local message = "You have been forced to AFK."
  2.  
  3. for k,v in pairs( target_ply ) do
  4.    
  5.     v:ConCommand( "ttt_spectator_mode 1" )
  6.     ULib.tsayError( target_ply, message )
  7.  
  8. end

but since it's now a PlayerArg (one person) I can't use that since it will ask for a table and it will find only one person.

How can I make it so that my
Code: Lua
  1. timer.Create( "AFK Timer", 1, 10, forceSpec() )
will activate the forceSpec() function and put them directly into spectator.

In other words, I need something that will, when called by the timer running out, will make the target_ply put into spectator.
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 #20 on: May 15, 2016, 02:02:51 pm »
Get rid of the loop.
Change v in front of the concommand to target_ply.
"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 #21 on: May 16, 2016, 02:26:18 pm »
Yeah that's what I thought too, however when I did that I got the error
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:8: attempt to index local 'target_ply' (a nil value)
  1. forceSpec - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:8
   2. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:12
what do I do here?

(Also, 50 posts ayy XD)
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 #22 on: May 16, 2016, 03:00:03 pm »
Post in code tags all that you've got now.
I don't want to have to flip pages just to see all your code/edits.
"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 #23 on: May 16, 2016, 03:04:48 pm »
sh_cc_cafk.lua:
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. local function forceSpec( calling_ply, target_ply )
  4.  
  5.         local message = "You have been forced to AFK."
  6.  
  7.     target_ply:ConCommand( "ttt_spectator_mode 1" )
  8.     ULib.tsayError( target_ply, message )
  9. end
  10.  
  11.  
  12. timer.Create( "AFK Timer", 1, 10, forceSpec() )
  13.  
  14. function ulx.cafk( calling_ply, target_ply )
  15.  
  16.   ULib.clientRPC( target_ply, "ulx.cafkClientSide" )
  17.   timer.Start( "AFK Timer" )
  18.   ulx.fancyLogAdmin( calling_ply, "#A is checking if #T is AFK.", target_ply )
  19.  
  20. end
  21.  
  22. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  23. cafk:addParam{ type=ULib.cmds.PlayerArg }
  24. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  25. cafk:help( "Used to check if a player is AFK." )

cl_cc_cafk.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.                 RunConsoleCommand( "say", "I'm not AFK." )
  47.                 afkBox:Close() 
  48.                 timer.Stop( "AFK Timer" )              
  49.         end
  50. end

This is what I have right now. Again, the error is:
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:8: attempt to index local 'target_ply' (a nil value)
  1. forceSpec - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:8
   2. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:12
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 #24 on: May 16, 2016, 04:52:19 pm »
I don't mean to come off as rude, but you really need to read your error messages...

I'll give you a rundown, if you don't quite understand them. The first line in any error message will contain [1] the path to the file (and line) that is causing the error, and [2] the error itself.

All subsequent lines will be steps in the stack trace. The stack trace is list of files and line numbers that were executed to get to the line that caused the error.

In your error, the first line is

Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:8: attempt to index local 'target_ply' (a nil value)
This tells us that there was an error on line 8 of addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua. The error was that the Lua engine tried to index a local value that was nil.

If we go to line 8 in addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua, we find this:

Code: Lua
  1. ULib.tsayError( target_ply, message )

According to the error, target_ply is nil. This probably means it is undefined, so our next step is to check for a definition of this variable. Go up line by line until you reach the end (technically the start) of the scope (the start of the function or the start of the file if it's not in a function), looking for a definition. Since this is in a function, we should also check the arguments of the function.

Code: Lua
  1. local function forceSpec( calling_ply, target_ply )

There it is! Now, target_ply is an argument to the function, so why is it throwing an error? Well, lets investigate further by going down the stack trace.

The second line of the error is this:

Code: [Select]
  1. forceSpec - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:8
This just tells us that it was executed by the forceSpec function, but we already know that. Onward...

Code: [Select]
   2. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:12
This line tells us that forceSpec was called from line 12 of the same file. Looking at that line, we find:

Code: Lua
  1. timer.Create( "AFK Timer", 1, 10, forceSpec() )

Okay, so it's a timer that is calling the forceSpec function. I still don't understand what's wrong...

Aha! forceSpec is being called without any arguments. In Lua, functions will still run even if they are not supplied with all their arguments. This means that target_ply will be nil in the function because it isn't being passed. To fix this, you're going to want to add a calling_ply and target_ply argument to your call of forceSpec.

TL;DR: Read your errors, they usually contain useful information. In this case it was a simple case of not supplying the needed arguments.
« Last Edit: May 16, 2016, 04:53:51 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 #25 on: May 16, 2016, 04:54:24 pm »
Ah, I just didn't know that the timer needed arguments in the function, that's all. Thanks

But I'm still kind of confused (sorry) how do I pass the arguments via the timer? Ugh this is confusing!

I made
Code: Lua
  1. timer.Create( "AFK Timer", 1, 10, forceSpec( calling_ply, target_ply ) )
but that still made the same error. I don't get what I'm doing wrong here  :-\

Hm.. so I got the errors to go away, all I did was put the timer inside the function forceSpec. However now the timer doesn't actually do anything (probably because it's in the function it's calling...) ugh I don't know how to fix this I'm so stupid.

Right now I have:
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. function forceSpec( calling_ply, target_ply )
  4.  
  5.         local message = "You have been forced to AFK."
  6.  
  7.     target_ply:ConCommand( "ttt_spectator_mode 1" )
  8.     ULib.tsayError( target_ply, message )
  9.  
  10.  
  11. end
  12.  
  13. function forceSpecTimers()
  14.  
  15. timer.Create( "AFK Timer", 1, 10, forceSpec(), target_ply )
  16.  
  17. end
  18. hook.Add( "Initialize", "Timers", forceSpecTimers )
  19.  
  20. function ulx.cafk( calling_ply, target_ply )
  21.  
  22.         ULib.clientRPC( target_ply, "ulx.cafkClientSide" )
  23.         timer.Start( "AFK Timer" )
  24.         ulx.fancyLogAdmin( calling_ply, "#A is checking if #T is AFK.", target_ply )
  25.  
  26. end
  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." )

I'm not getting any errors, just the timer isn't working. How can I fix this? I don't know if it's because I'm tired or what but I just can't figure this out.
« Last Edit: May 16, 2016, 05:34:29 pm 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 #26 on: May 16, 2016, 08:10:41 pm »
Okay, lets start by showing an example timer usage:

Code: Lua
  1. timer.Create( "UniqueName", 1, 1, function() myFunction( myArgument ) end )

This will create a timer with the name "UniqueName" that will wait one second and then run once, calling myFunction( myArgument). You seem to think that you pass the arguments after the function, or in normal function notation. This doesn't work, instead you want to create a dummy function that can contain your other function calls.

Before we go into why your code isn't doing anything, I want you to ask yourself why you have the 'calling_ply' argument for forceSpec. You're not using it anywhere in the function, so what's the point?

Now even if you used the correct format for passing the function and its arguments, it still wouldn't work (you'd probably get an error, actually). This is because you're trying to pass the variable target_ply which doesn't exist. You haven't defined it in your function body, nor in forceSpecTimers(). If you want to force spectate someone, you have to specify who.

Also, I'm pretty sure timer.Create() automatically starts the timer, so your timer.Start() is redundant (and your code might not work as expected). Don't take my word on this though, because I haven't worked with timers in a while.
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 #27 on: May 17, 2016, 01:31:28 pm »
So I tried that
Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. function forceSpec( target_ply )
  4.  
  5.         message = "You have been forced to AFK."
  6.  
  7.     target_ply:ConCommand( "ttt_spectator_mode 1" )
  8.     ULib.tsayError( target_ply, message )
  9.  
  10.  
  11. end
  12.  
  13. function ulx.cafk( calling_ply, target_ply )
  14.  
  15.         ULib.clientRPC( target_ply, "ulx.cafkClientSide" )
  16.         ulx.fancyLogAdmin( calling_ply, "#A is checking if #T is AFK.", target_ply )
  17.         timer.Create( "AFK Timer", 1, 10, forceSpec( target_ply ) )
  18.  
  19. end
  20.  
  21. timer.Create( "AFK Timer", 1, 10, function forceSpec( target_ply ) )
  22.  
  23. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  24. cafk:addParam{ type=ULib.cmds.PlayerArg }
  25. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  26. cafk:help( "Used to check if a player is AFK." )
  27.  

But I kept getting
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:21: unexpected symbol near ')'
  1. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:0

So I figured, maybe it needs to be "function forceSpec( target_ply )" instead of "function() forceSpec( target_ply )" so I tried that and then got
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:21: '(' expected near 'forceSpec'
  1. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:0


And what does "This is because you're trying to pass the variable target_ply which doesn't exist. You haven't defined it in your function body..." mean? How do I do that
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 #28 on: May 17, 2016, 03:04:07 pm »
-function error-

You forgot to end your function.

And what does "This is because you're trying to pass the variable target_ply which doesn't exist. You haven't defined it in your function body..." mean? How do I do that

You tried to provide the function with an argument, a variable called 'target_ply'. The variable 'target_ply' was not defined, so it would have given you an error. You can't just pass it a random, undefined variable and expect it to work... you need to supply it with a player.
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 #29 on: May 17, 2016, 03:24:31 pm »
How do I assign target_ply to the person I'm targeting though?


Code: Lua
  1. local CATEGORY_NAME = "Utility"
  2.  
  3. function forceSpec( target )
  4.  
  5.        
  6.  
  7.         message = "You have been forced to AFK."
  8.  
  9.     target_ply:ConCommand( "ttt_spectator_mode 1" )
  10.     ULib.tsayError( target_ply, message )
  11.  
  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. end
  21.  
  22. timer.Create( "AFK Timer", 1, 10, function() forceSpec( target_ply ) end )
  23.  
  24. local cafk = ulx.command( CATEGORY_NAME, "ulx cafk", ulx.cafk, "!cafk" )
  25. cafk:addParam{ type=ULib.cmds.PlayerArg }
  26. cafk:defaultAccess( ULib.ACCESS_OPERATOR )
  27. cafk:help( "Used to check if a player is AFK." )

Also, does the "Timer Failed!" error come when something like this happens?
Code: [Select]
[ERROR] addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:9: attempt to index global 'target_ply' (a nil value)
  1. forceSpec - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:9
   2. unknown - addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua:22
Like is that error (because it's nil) causing the Timer Failed! error to show up too?
if not, how do I fix that?
Code: [Select]
Timer Failed! [AFK Timer][@addons/customcommands/lua/ulx/modules/sh/sh_cc_cafk.lua (line 22)]
« Last Edit: May 17, 2016, 03:34:49 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print