Author Topic: DarkRP Refund Command  (Read 310 times)

0 Members and 1 Guest are viewing this topic.

Offline [?] The Last Centurion

  • Newbie
  • *
  • Posts: 15
  • Karma: 2
    • View Profile
DarkRP Refund Command
« on: October 21, 2017, 10:41:40 PM »
So I haven't done Lua in a while, so I'm basically new to it. I'm trying to make a command that would take the input of a target and whatever number specified.




Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2. local money
  3.  
  4. function ulx.refund( calling_ply, target_ply, money )
  5.         if calling_ply == target_ply then
  6.                 DarkRP.notify(calling_ply, 1, 5, "You cannot refund yourself.")
  7.         else if money < 0 then
  8.                 DarkRP.notify(calling_ply, 1, 5, "You must enter a number higher than 0!")
  9.         else
  10.                 target_ply:GiveMoney( money )
  11.         end
  12. end
  13.  
  14. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund", true)
  15. refund:addParam{ type=ULib.cmds.PlayerArg }
  16. refund:defaultAccess( ULib.ACCESS_ADMIN )
  17. refund:help( "Refunds a player the amount specified" )

This is what I have, it should give enough information or example of what I want. I just do not know how to make it work. I currently get an error with line 7 saying "addons/ulx/lua/ulx/modules/sh/refund.lua:5: attempt to compare nil with number" (says line 5 because i added the if statement above it after I got the error).

Offline Timmy

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 155
  • Karma: 127
  • Code monkey
    • View Profile
Re: DarkRP Refund Command
« Reply #1 on: October 22, 2017, 05:45:31 AM »
Make sure to tell ULX that your command takes a numeric parameter as well:
Code: Lua
  1. refund:addParam{ type=ULib.cmds.NumArg, hint="money", ULib.cmds.round }

Tip: You can choose to hand validation off to ULX:

- Add min=1 if you want the amount of money to always be above 0.
Code: Lua
  1. refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round }
- Add target="!^" if you don't want players to target themselves.
Code: Lua
  1. refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
See Commands on the ULib documentation for more information on restrictions.
« Last Edit: October 22, 2017, 05:47:06 AM by Timmy »
hello@timmy.ws (PGP key) • Steam • GitHub

Offline [?] The Last Centurion

  • Newbie
  • *
  • Posts: 15
  • Karma: 2
    • View Profile
Re: DarkRP Refund Command
« Reply #2 on: October 22, 2017, 07:34:18 AM »
Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2. local money
  3.  
  4. function ulx.refund( target_ply, money )
  5.                 target_ply:GiveMoney( money )
  6.         end
  7.  
  8. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund", true)
  9. refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round, target_ply="!^" }
  10. refund:defaultAccess( ULib.ACCESS_ADMIN )
  11. refund:help( "Refunds a player the amount specified" )

This is the updated code, when typing "!refund (amount) (name)" I get the error "addons/ulx/lua/ulx/modules/sh/refund.lua:5: attempt to index local 'target_ply' (a number value)"


EDIT: I'm trying to fix errors as i get them, will update if it works.

EDIT2: I currently get "addons/ulx/lua/ulx/modules/sh/refund.lua:5: attempt to call method 'AddMoney' (a nil value)" and "addons/ulx/lua/ulx/modules/sh/refund.lua:5: attempt to call method 'GiveMoney' (a nil value)" as errors.
« Last Edit: October 22, 2017, 08:07:01 AM by [?] The Last Centurion »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7759
  • Karma: 353
  • Sertafide Ulysses Jenius
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
Re: DarkRP Refund Command
« Reply #3 on: October 22, 2017, 08:24:09 AM »
Few things to research:
--
Is this a server side only command? Or a client side only command?
Though it can still live in /sh (shared), if it's either one or the other, you'd need to specify a comparison to ensure it's only being run where it's supposed to (server or client).
--
Do you have any command anywhere else that is trying to run this command as the game starts?
It's possible ULX might have loaded this part of code before DarkRP initialized it's code, and is being run too soon, before the DarkRp lua exists yet.
--
Tried using format GiveMoney( target_ply, money)?
I've never fully understood the technical why, but I've seen some commands not allow the <target>:<Command>(variable) format.

The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin

Offline Timmy

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 155
  • Karma: 127
  • Code monkey
    • View Profile
Re: DarkRP Refund Command
« Reply #4 on: October 22, 2017, 08:50:50 AM »
Make sure to double-check your command setup. You will get odd errors because your command arguments are not being parsed correctly. You intend to have a command that takes 2 parameters, but only have a single cmd:addParam instruction.

Start with a tiny piece of working code. Make small changes and verify that they work correctly. This makes it much easier to pinpoint mistakes! Use print to display the contents of your arguments in the server console.

1. Let's start with the basic command template without parameters.
Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2.  
  3. function ulx.refund( calling_ply )
  4.     print( "Command called by", calling_ply )
  5. end
  6. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
  7. refund:defaultAccess( ULib.ACCESS_ADMIN )
  8. refund:help( "Refunds a player the amount specified" )

2. Now let's set up the first parameter for the the command: a player (not self).
Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2.  
  3. function ulx.refund( calling_ply, target_ply )
  4.     print( "Command called by", calling_ply )
  5.     print( "We're targeting", target_ply )
  6. end
  7. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
  8. refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
  9. refund:defaultAccess( ULib.ACCESS_ADMIN )
  10. refund:help( "Refunds a player the amount specified" )

3. Now let's set up the second parameter for the the command: a number (bigger than 0, integer).
Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2.  
  3. function ulx.refund( calling_ply, target_ply, money )
  4.     print( "Command called by", calling_ply )
  5.     print( "We're targeting", target_ply )
  6.     print( "We're refunding", money )
  7. end
  8. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
  9. refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
  10. refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round }
  11. refund:defaultAccess( ULib.ACCESS_ADMIN )
  12. refund:help( "Refunds a player the amount specified" )

4. Now that we've verified our arguments are parsed correctly, we can refund the player. Looks like the correct function to give money to a player is Player:addMoney.
Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2.  
  3. function ulx.refund( calling_ply, target_ply, money )
  4.     target_ply:addMoney( money )
  5. end
  6. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
  7. refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
  8. refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round }
  9. refund:defaultAccess( ULib.ACCESS_ADMIN )
  10. refund:help( "Refunds a player the amount specified" )
« Last Edit: October 22, 2017, 08:54:09 AM by Timmy »
hello@timmy.ws (PGP key) • Steam • GitHub

Offline [?] The Last Centurion

  • Newbie
  • *
  • Posts: 15
  • Karma: 2
    • View Profile
Re: DarkRP Refund Command
« Reply #5 on: October 22, 2017, 11:01:30 AM »
Is this a server side only command? Or a client side only command?
Though it can still live in /sh (shared), if it's either one or the other, you'd need to specify a comparison to ensure it's only being run where it's supposed to (server or client).

Wouldn't it have to run on the server as you'd be giving another player the money? How would I go about specifying a comparison?

Other than that, I actually think it would have worked, but I made it "AddMoney" instead of "addMoney".

-snip-

Thank you, and if I do more Lua in the future, I will keep that print line thing in mind.



Another question, how would I confirm that it's actually working?

Offline Timmy

  • Respected Community Member
  • Full Member
  • *****
  • Posts: 155
  • Karma: 127
  • Code monkey
    • View Profile
Re: DarkRP Refund Command
« Reply #6 on: October 23, 2017, 10:43:12 AM »
Another question, how would I confirm that it's actually working?

Try to refund yourself from the server console:
Code: [Select]
ulx refund Centurion 100
There won't be any feedback, but the money should be added to your wallet.

Last step would be to use ulx.fancyLogAdmin to log the action and deliver some feedback when you run the command:
Code: Lua
  1. local CATEGORY_NAME = "Admin"
  2.  
  3. function ulx.refund( calling_ply, target_ply, money )
  4.     target_ply:addMoney( money )
  5.     ulx.fancyLogAdmin( calling_ply, "#A refunded $#i to #T", money, target_ply )
  6. end
  7. local refund = ulx.command( CATEGORY_NAME, "ulx refund", ulx.refund, "!refund" )
  8. refund:addParam{ type=ULib.cmds.PlayerArg, target="!^" }
  9. refund:addParam{ type=ULib.cmds.NumArg, hint="money", min=1, ULib.cmds.round }
  10. refund:defaultAccess( ULib.ACCESS_ADMIN )
  11. refund:help( "Refunds a player the amount specified" )
hello@timmy.ws (PGP key) • Steam • GitHub

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 7759
  • Karma: 353
  • Sertafide Ulysses Jenius
    • View Profile
    • Team Ulysses [ULib/ULX, other fine releases]
Re: DarkRP Refund Command
« Reply #7 on: October 23, 2017, 06:31:46 PM »
Wouldn't it have to run on the server as you'd be giving another player the money? How would I go about specifying a comparison?
In most situations of permission based code, yes, but that doesn't mean it may not error out with 'unknown function' or 'nil function' if being run on the wrong side. :)
You could easily use
Code: [Select]
if SERVER then
    ... server based functions and code
else
    ... client based functions and code
end
when using code in shared files.
The price one pays for pursuing any profession or calling is an intimate knowledge of its ugly side. - James Baldwin