• Print

Author Topic: Unable to figure out why my commands are not working.  (Read 9515 times)

0 Members and 1 Guest are viewing this topic.

Offline MaDmaxwell

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Unable to figure out why my commands are not working.
« on: March 03, 2016, 09:14:08 am »
Hello, I am in the process of writing some custom commands on my RP server. I know that the command is registered in ULX as I am getting the following errors:
[ERROR] ulx_custom/lua/ulx/modules/sh/customcommands.lua:294 ulx.respawn attempt to use #target_plys a usergroup value.
 
[ERROR] ulx_custom/lua/ulx/modules/sh/customcommands.lua:316 ulx.npcnotarget attempt to use #target_plys a usergroup value.

Here is the code for both of them.
Code: Lua
  1. function ulx.respawn( calling_ply, target_plys )
  2.  
  3.         local affected_plys = {}
  4.  
  5.         for i = 1, #target_plys do
  6.                
  7.                 local v = target_plys[ i ]
  8.  
  9.                 v:Spawn()
  10.  
  11.                 table.insert( affected_plys, v )
  12.  
  13.         end
  14.  
  15.         ulx.fancyLogAdmin( calling_ply, "#A respawned #T", affected_plys )
  16.  
  17. end
  18. local respawn = ulx.command( "Utility", "ulx respawn", ulx.respawn, "!respawn" )
  19. respawn:addParam{ type=ULib.cmds.PlayersArg }
  20. respawn:defaultAccess( ULib.ACCESS_ADMIN )
  21. respawn:help( "Respawns the target" )
  22.  
  23. function ulx.npcnotarget( calling_ply, target_plys, should_revoke )
  24.        
  25.         local affected_plys = {}
  26.  
  27.         for i = 1, #target_plys do
  28.  
  29.                 local v = target_plys[ i ]
  30.  
  31.                 if should_revoke then
  32.  
  33.                         v:SetNoTarget( false )
  34.  
  35.                         table.insert( affected_plys, v )
  36.  
  37.                 elseif not should_revoke then
  38.  
  39.                         v:SetNoTarget( true )
  40.  
  41.                         table.insert( affected_plys, v )
  42.  
  43.                 end
  44.  
  45.         end
  46.  
  47.         ulx.fancyLogAdmin( calling_ply, "#A toggled npc targeting #T", affected_plys )
  48.  
  49. end
  50. local notarget = ulx.command( "Utility", "ulx npcnotarget", ulx.npcnotarget, "!notarget" )
  51. notarget:addParam{ type=ULib.cmds.PlayersArg }
  52. notarget:defaultAccess( ULib.ACCESS_ADMIN )
  53. notarget:help( "Disables NPC Targeting to the target." )
  54. notarget:setOpposite( "ulx unnpcnotarget", {_,_, true}, "!unnotarget" )
  55.  
Sorry for the messy code, any help is appreciated. If someone could show me how to fix this code I would be highly appreciative.
« Last Edit: March 03, 2016, 05:20:40 pm by MaDmaxwell »

Offline [Dev]HEROBRINE | trade.tf

  • Newbie
  • *
  • Posts: 6
  • Karma: 0

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Unable to figure out why my commands are not working.
« Reply #2 on: March 03, 2016, 12:42:55 pm »
Without knowing which lines the error is referring to it is hard to help you. In this case it is fairly obvious as there are only two lines in which #target_plys is used but for future please give us the line numbers relative to the code you give us to make it easier for us to help you.

Your functions' second arguments are target_plys. Usually this is used for commands that can target multiple people, but you are telling ULX that it can only handle one player by saying that the type of the argument is ULib.cmds.PlayerArg instead of ULib.cmds.PlayersArg. Switching it to ULib.cmds.PlayersArg should get rid of that error.
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 roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Unable to figure out why my commands are not working.
« Reply #3 on: March 03, 2016, 12:45:30 pm »
There is no reason to double post. I'm not sure if the last one is in the wrong area, but if it is a staff member will move it.
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 MaDmaxwell

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Unable to figure out why my commands are not working.
« Reply #4 on: March 03, 2016, 12:48:33 pm »
Okay, I am sorry, my past experience has been that the post gets deleted but oh well, live and learn. To the staff out there, I apologize that I did not read and I double posted when you could have moved it.     

Offline feldma

  • Newbie
  • *
  • Posts: 47
  • Karma: 5
  • 5696 hours in Gmod and counting!
    • Mega-Strike Network
Re: Unable to figure out why my commands are not working.
« Reply #5 on: March 03, 2016, 01:36:50 pm »
Just remove the # from the for i = 1, #target_ply.
Solving 50% of people's questions by searching it up on google.

Trying to think of a cool idea to code, so I can inspire myself to code. If you want something done, please message me!

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Unable to figure out why my commands are not working.
« Reply #6 on: March 03, 2016, 02:22:47 pm »
Just remove the # from the for i = 1, #target_ply.
uh... No. That's the list length operator in Lua.

OP, take a look at your commands. You expect a table of players, target_plys, but you have a PlayerArg parameter specified below. Change PlayerArg to PlayersArg, assuming you want to run it on multiple people.
bw81@ulysses-forums ~ % whoami
Homepage

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Unable to figure out why my commands are not working.
« Reply #7 on: March 03, 2016, 03:44:01 pm »
...and this is why we don't start two threads.

As I said in the other thread, and as Bytewave has said here, the problem is he is treating a userdata (target_plys is referring to a player) as a table because he isn't telling ULX to use the PlayersArg and is instead telling it to use PlayerArg.

I'm legitimately interested as to why you think removing the pound sign would work though, feldma. (I tried very hard to resist the urge to calling it a hashtag, I must admit.)
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 MaDmaxwell

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Unable to figure out why my commands are not working.
« Reply #8 on: March 03, 2016, 04:24:42 pm »
Thank you all. I have edited the file and waiting on a map switch. Thanks again.

Offline MaDmaxwell

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Unable to figure out why my commands are not working.
« Reply #9 on: March 03, 2016, 05:16:27 pm »
Okay the respawn command works now, and so does the NoTarget command, only thing is that when I do the opposite command for notarget it does not un-notarget me. The command runs fine, no errors at all yet it is not disabling the notarget. Furthermore, when killing myself and doing !unnotarget %Owner which is the group I am in, NPCs still do not target me and will not shoot me. Here is the updated code for the ulx.npcnotarget command:
Code: Lua
  1. function ulx.npcnotarget( calling_ply, target_plys, should_revoke )
  2.        
  3.         local affected_plys = {}
  4.  
  5.         for i = 1, #target_plys do
  6.  
  7.                 local v = target_plys[ i ]
  8.  
  9.                 if should_revoke then
  10.  
  11.                         v:SetNoTarget( false )
  12.  
  13.                         table.insert( affected_plys, v )
  14.  
  15.                 elseif not should_revoke then
  16.  
  17.                         v:SetNoTarget( true )
  18.  
  19.                         table.insert( affected_plys, v )
  20.  
  21.                 end
  22.  
  23.         end
  24.  
  25.         ulx.fancyLogAdmin( calling_ply, "#A toggled npc targeting #T", affected_plys )
  26.  
  27. end
  28. local notarget = ulx.command( "Utility", "ulx npcnotarget", ulx.npcnotarget, "!notarget" )
  29. notarget:addParam{ type=ULib.cmds.PlayersArg }
  30. notarget:defaultAccess( ULib.ACCESS_ADMIN )
  31. notarget:help( "Disables NPC Targeting to the target." )
  32. notarget:setOpposite( "ulx unnpcnotarget", {_,_, true}, "!unnotarget" )
  33.  

Offline feldma

  • Newbie
  • *
  • Posts: 47
  • Karma: 5
  • 5696 hours in Gmod and counting!
    • Mega-Strike Network
Re: Unable to figure out why my commands are not working.
« Reply #10 on: March 03, 2016, 09:33:33 pm »
I think (and since my coding is still horrible), the reason the opposite command isn't working is because you have an
Code: [Select]
elseif not should_revoke.
I don't think that is how the opposite command works.

But as I say, I can't code (yet).
Solving 50% of people's questions by searching it up on google.

Trying to think of a cool idea to code, so I can inspire myself to code. If you want something done, please message me!

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Unable to figure out why my commands are not working.
« Reply #11 on: March 04, 2016, 04:19:26 am »
I think (and since my coding is still horrible), the reason the opposite command isn't working is because you have an
Code: [Select]
elseif not should_revoke.
I don't think that is how the opposite command works.

But as I say, I can't code (yet).

If should_revoke works how I think it works (which it probably doesn't), then their code should be fine. One possibility is that SetNoTarget isn't/doesnt do what you are expecting. Try putting a print (or a fancyLogAdmin) in the if statements for should_revoke and see what prints.
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 Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Unable to figure out why my commands are not working.
« Reply #12 on: March 04, 2016, 07:32:49 am »
only thing is that when I do the opposite command for notarget it does not un-notarget me. The command runs fine, no errors at all yet it is not disabling the notarget.
For setOpposite to work:

- Add a boolean parameter to your command. This is necessary in order to detect if the calling_ply wants to run the opposite version of the command.
Code: Lua
  1. example:addParam{ type=ULib.cmds.BoolArg, invisible=true }

- Add this new parameter to your callback function (I called mine "opposite").
Code: Lua
  1. function ulx.example( calling_ply, opposite )

- Define the opposite version of your command.
Code: Lua
  1. example:setOpposite( "ulx unexample", { nil, true }, "!unexample" )
The second parameter of setOpposite allows you to override the parameters in the callback function. The values in that table (nil, true) correspond to the parameters of the callback (calling_ply, opposite). Use nil if you want to keep the original value. Use anything else to override the original value.
In the case of the example, we want to keep the original value of "calling_ply" but override "opposite" to be true.

You didn't add the boolean parameter to your command so ULX ignores the fact that you're trying to set "should_revoke" to "true" with setOpposite.
« Last Edit: March 04, 2016, 07:40:52 am by Timmy »

Offline MaDmaxwell

  • Newbie
  • *
  • Posts: 8
  • Karma: 1
Re: Unable to figure out why my commands are not working.
« Reply #13 on: March 04, 2016, 10:35:49 am »
Question why would I use nil instead of _? The default ULX commands use this:
Code: Lua
  1. ignite:setOpposite( "ulx unignite", {_, _, _, true}, "!unignite" )

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Unable to figure out why my commands are not working.
« Reply #14 on: March 04, 2016, 10:45:22 am »
Question why would I use nil instead of _? The default ULX commands use this:
Code: Lua
  1. ignite:setOpposite( "ulx unignite", {_, _, _, true}, "!unignite" )

Stylistic choice.
_ is a valid variable name in Lua, so people often use it in place of nil because it's meaningless and typically undefined. Plus it's easy to type.
bw81@ulysses-forums ~ % whoami
Homepage

  • Print