Jump to content

[HELP] addCommandHandler


Recommended Posts

Hi all.

I am new on scripting but here's my question.

the lua sytnax of "addCommandHandler"

is :

addCommandHandler ("CommandName", CommandFunction ) 

in this case the player will type /commandName

but i'd like to make a command where the player must do for exemple /spectate playerName

i mean the command will have two parts . i'd like to know how i do that.

Link to comment

As you can see on the wiki under "Handler function parameters" :

player playerSource, string commandName, [string arg1, string arg2, ...]

So:

addCommandHandler ( "spectate",  
function (player, cmd, playername ) 
    -- your code 
end) 

So you can excecute "playername" as the text what you have typed in game like:

"/spectate [DenR]Danny"

So now 'playername' will return "[DenR]Danny"

if the argument isn't specifided it will return "nil"

So you actually need to chek that argument:

addCommandHandler ( "spectate",  
function (player, cmd, playername ) 
    if ( playername ) then 
        outputChatBox ( playername ) 
    end 
end) 

edit:

You want to spectate the player I guess, then these functions might help you:

  
setCameraTarget 
getPlayerFromName 
  

Maybe some usefull functions that might help you to improve your code:

https://wiki.multitheftauto.com/wiki/Get ... omNamePart

Edited by Guest
Link to comment

Well, I'll explain it better:

  
function specPlayer( player, cmd, typedArgument )  
--player = the player who typed the command 
--cmd = the command name 
--typedArgument is a argument what you can pass with commandHandler 
  
end -- adding a end to close the function 
addCommandHandler ( "spectate", specPlayer ) 

When the player now typs: "/spectate danny" it will returnt typedArgument, because see here:

https://wiki.multitheftauto.com/wiki/AddCommandHandler

scroll down to "Handler function parameters"

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...