(ULX COMMAND)Why doesnt this work and how do i make it work?

Hi. Just spent some time working on a ulx command, but it will not work. This command is next round slay. Basically, i have a (excl’s) jailbreak server and was making commands but this one has me confused. Here is the code:



function ulx.nrslay( calling_ply, target_ply )
     local affected_plys = {}
     
     for i=1, #target_plys do
          local v = target_plys[ 1 ]

          if ulx.getExclusive( v, calling_ply ) then
               ULib.tsayError( calling_player, ulx.getExclusive( v, calling_ply ), true )
          elseif not  v:Alive() then
               ULib.tsayError( calling_ply, v:Nick() .. " is already dead!" true )
          elseif v:IsFrozen()then
               ULib.tsayError( calling_ply, v:Nick() .. " is Frozen!", true) 
          else
               hook.add( NewRound, nrslay1, nrslay1 ) ##Note:  "NewRound" is a excl's jailbreak event, GM:NewRound
          end
     end
end

     function nrslay1( calling_ply, target_ply )
          local affected_plys = {}
          local v = target_plys[ 1 ]

          v:Kill()
          table.insert( affected_plys, v )
          ulx.fancyLogAdmin( calling_ply, "#A slayed #T for next round.", affected_plys )
end

local nrslay = ulx.command( CATEGORY_NAME, "ulx nrslay", ulx.nrslay, "!nrslay", true )
nrslay:addParam{ type=ULib.cmds.PlayerArg }
nrslay:defaultAccess( ULib.ACCESS_ADMIN )
nrslay:help( "Slays the player next round" )


I do not get any error while running this, please point out typos (I had to retype it so there might be some typos, then again - it could be from the script).

at a glance ( though this might not be everything )


hook.add( NewRound, nrslay1, nrslay1 )

should be


hook.add( "NewRound", "nrslay1", nrslay1 )

There are many many reasons for why this code is wrong, I’ll address them by their order of appearance:

  • Your command argument is target_ply, yet you address target_plys. Do you want this command to have a single target or multiple ones?
  • If you DO want to have multiple targets - you will always address the first one either way, since you state v = target_plys[ 1 ] instead of target_plys[ i ]
  • On line 10 of your code, the “true” argument is part of your text, i.e. you are missing a comma.
  • You’re calling hook.Add in a function - this will not work.

Now about the external function part - there are many errors with this so I’ll try to offer an alternative before helping you fix it:
Why even have an external command? take the code inside nrslay1 and put it under “else”, there’s no need to use a command for it.

In case you do want to do it like that, I’ll to explain the general way this works:
An external command is addressed by simply typing the command name (instead of hook.Add( …, nrslay1) it’s just nrslay1( args ))
Also - you need to pass your arguments when calling the function.

I have done the majority of what you said. However, i do not know how this external command will work. You say it addresses the external command, does that mean i am to call the external function? If not how will i make it wait till next round in order to use?

You need to completely remove the hook and command from inside the ulx function.
Set a variable to all affected players, and have the command ran by the hook each new round, checking who needs to get slain - and slaying them

What is i make a function inside the hook(and move it out of the function)

i know i can make a function inside of a hook, so what if i tried something like this (assume i kept the beginning and end):



function ulx.nrslay( calling_ply, target_ply )
     local affected_plys = {}
     
     for i=1, #target_plys do
          local v = target_plys[ 1 ]

          if ulx.getExclusive( v, calling_ply ) then
               ULib.tsayError( calling_player, ulx.getExclusive( v, calling_ply ), true )
          elseif not  v:Alive() then
               ULib.tsayError( calling_ply, v:Nick() .. " is already dead!" true )
          elseif v:IsFrozen()then
               ULib.tsayError( calling_ply, v:Nick() .. " is Frozen!", true) 
          end
     end
end

     hook.add("NewRound", "nrslay1", (function nrslay1( calling_ply, target_ply )
          local affected_plys = {}
          local v = target_plys[ 1 ]

          v:Kill()
          table.insert( affected_plys, v )
          ulx.fancyLogAdmin( calling_ply, "#A slayed #T for next round.", affected_plys )
end)



First of all, there’s no need to give the function a name one you’ve called it inside a hook, and you also have an unnecessary ( that I don’t understand why it’s there really (Before “function nrslay1”)

You also have target_ply as your argument and yet you refer to target_plys in your code.
Also, if you want to target multiple players - assuming you kept the same code from before (The part I can’t see) - You have a PlayerArg parameter when it should be PlayersArg