Author Topic: Trying to make a command  (Read 4733 times)

0 Members and 2 Guests are viewing this topic.

Offline (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Trying to make a command
« on: July 08, 2016, 02:06:15 PM »
My ULib/ULX versions (run "ulx version" in console): Won't work? I have the latest...
ULib v..
ULX v..

Game mode(s) I am having this problem on: Deathrun

Lua errors shown in console, if any:
Code: [Select]
<insert errors here>

I know this may sound stupid but there have been tons of request on my server for a command that gives someone cookies. I have made commands before but I don't know how to do this one. I don't need it to store an amount of cookies within the server files but just print into chat something like, "#A gave #T "number" cookies!" I just need help making it please.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Trying to make a command
« Reply #1 on: July 08, 2016, 03:08:32 PM »
So... there's no other use than to just print a chat message?
If so, that's just as simple as a PlayerArg, a (rounded) NumberArg, and a fancyLogAdmin with the #i token. While it uses a PlayersArg, ulx slap should be easy to modify (remove function body save for the fancyLogAdmin call, rename target_plys to target_ply for consistency, change PlayersArg to PlayerArg, and edit the message string, keeping the tokens in-tact).
« Last Edit: July 08, 2016, 03:14:37 PM by Bytewave »
bw81@ulysses-forums ~ % whoami
Homepage

Offline (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Trying to make a command
« Reply #2 on: July 08, 2016, 10:25:04 PM »
So... there's no other use than to just print a chat message?
If so, that's just as simple as a PlayerArg, a (rounded) NumberArg, and a fancyLogAdmin with the #i token. While it uses a PlayersArg, ulx slap should be easy to modify (remove function body save for the fancyLogAdmin call, rename target_plys to target_ply for consistency, change PlayersArg to PlayerArg, and edit the message string, keeping the tokens in-tact).

Any way you can help me with the code itself? I have no idea how I would structure this....

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Trying to make a command
« Reply #3 on: July 09, 2016, 12:57:29 AM »
Look at the code for the default commands. You should be able to copy paste the code and change it to match your needs.

The code for commands is located in garrysmod/addons/ulx/lua/ulx/modules/sh/. You can also view the commands' code online.
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 (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Trying to make a command
« Reply #4 on: July 09, 2016, 12:59:10 AM »
Look at the code for the default commands. You should be able to copy paste the code and change it to match your needs.

The code for commands is located in garrysmod/addons/ulx/lua/ulx/modules/sh/. You can also view the commands online.

Again, I know how to make commands, it is just this one thing I am getting hung up on. I have a few commands already, I just don't know how to do the number thing. Like the gold-star command on your old server roast.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Trying to make a command
« Reply #5 on: July 09, 2016, 01:04:17 AM »
Try to get as far as you can on the command, and come ask for help if you get stuck.

If you've already gotten stuck, it would be helpful to see your code.
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 (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Trying to make a command
« Reply #6 on: July 09, 2016, 01:06:04 AM »
I don't know how to add in a ply or something of the sort for numbers. like how you have calling play and target ply, what do you use for numbers?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Trying to make a command
« Reply #7 on: July 09, 2016, 01:12:12 AM »
As Bytewave suggested, take a look at the slap command' code. It has a number argument.
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 (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Trying to make a command
« Reply #8 on: July 09, 2016, 01:15:38 AM »
I'm doing that right now

Offline (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Trying to make a command
« Reply #9 on: July 09, 2016, 01:28:45 AM »
Code: [Select]
CATEGORY_NAME = "Custom Commands by Pot"

function ulx.cookie( calling_ply, target_ply, dmg )
local affected_plys = {}

for i=1, #target_ply do
local v = target_ply[ i ]
if v:IsFrozen() then
ULib.tsayError( calling_ply, v:Nick() .. " is frozen!", true )
else
ULib.cookie( v, dmg )
table.insert( affected_plys, v )
end
end

ulx.fancyLogAdmin( calling_ply, "#A gave #T #i cookies!", affected_plys, dmg )
end

local cookie = ulx.command( CATEGORY_NAME, "ulx cookie", ulx.cookie, "!cookie" )
cookie:addParam{ type=ULib.cmds.PlayerArg }
cookie:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="damage", ULib.cmds.optional, ULib.cmds.round }
cookie:defaultAccess( ULib.ACCESS_ADMIN )
cookie:help( "Gives someone cookies!" )

That is what I have, but it still wont work. I tried to do what Bytewave said but it was confusing for me. I am still a little new to working with ULX

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: Trying to make a command
« Reply #10 on: July 09, 2016, 09:17:38 AM »
Code: [Select]
CATEGORY_NAME = "Custom Commands by Pot"

function ulx.cookie( calling_ply, target_ply, dmg )
local affected_plys = {}

for i=1, #target_ply do
local v = target_ply[ i ]
if v:IsFrozen() then
ULib.tsayError( calling_ply, v:Nick() .. " is frozen!", true )
else
ULib.cookie( v, dmg )
table.insert( affected_plys, v )
end
end

ulx.fancyLogAdmin( calling_ply, "#A gave #T #i cookies!", affected_plys, dmg )
end

local cookie = ulx.command( CATEGORY_NAME, "ulx cookie", ulx.cookie, "!cookie" )
cookie:addParam{ type=ULib.cmds.PlayerArg }
cookie:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="damage", ULib.cmds.optional, ULib.cmds.round }
cookie:defaultAccess( ULib.ACCESS_ADMIN )
cookie:help( "Gives someone cookies!" )

That is what I have, but it still wont work. I tried to do what Bytewave said but it was confusing for me. I am still a little new to working with ULX
Again, I said gut the function save for the call to ulx.fancyLogAdmin.
I tried to break everything down for you. I feel like I did everything but write it for you in that post, but I'll try again based on the code you have now.

Remove everything between function ulx.cookie( calling_ply, target_ply, dmg ) and ulx.fancyLogAdmin( calling_ply, "#A gave #T #i cookies!", affected_plys, dmg ).
Replace all occurrences of dmg with cookies for consistency.
Replace affected_plys in the ulx.fancyLogAdmin call with target_ply --- affected_plys no longer exists and isn't waht you want anyway.
Replace "damage" in the hint section of the second cookie:addParam call with "cookies" for consistency.
Done.
bw81@ulysses-forums ~ % whoami
Homepage

Offline (TG) PotLuck

  • Newbie
  • *
  • Posts: 9
  • Karma: 0
Re: Trying to make a command
« Reply #11 on: July 09, 2016, 10:18:25 AM »
Again, I said gut the function save for the call to ulx.fancyLogAdmin.
I tried to break everything down for you. I feel like I did everything but write it for you in that post, but I'll try again based on the code you have now.

Remove everything between function ulx.cookie( calling_ply, target_ply, dmg ) and ulx.fancyLogAdmin( calling_ply, "#A gave #T #i cookies!", affected_plys, dmg ).
Replace all occurrences of dmg with cookies for consistency.
Replace affected_plys in the ulx.fancyLogAdmin call with target_ply --- affected_plys no longer exists and isn't waht you want anyway.
Replace "damage" in the hint section of the second cookie:addParam call with "cookies" for consistency.
Done.


Um, I did what you said. I thing the #i is messing up the fancy log admin print. Do I need it back? If so, where do I put it.

Code: [Select]
CATEGORY_NAME = "Custom Commands by Pot"

function ulx.cookie( calling_ply, target_ply, cookies )

ulx.fancyLogAdmin( calling_ply, "#A gave #T #i cookies!", affected_ply, cookies )
end

local cookie = ulx.command( CATEGORY_NAME, "ulx cookie", ulx.cookie, "!cookie" )
cookie:addParam{ type=ULib.cmds.PlayerArg }
cookie:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="cookies", ULib.cmds.optional, ULib.cmds.round }
cookie:defaultAccess( ULib.ACCESS_ADMIN )
cookie:help( "Gives someone cookies!" )

An Error Has Occurred!

array_keys(): Argument #1 ($array) must be of type array, null given