Jump to content

Command execution with "!"


Shido

Recommended Posts

Hey there, I'm on a command script and i wanted to make commands executable with the "!" character apart of the ususal "/"

For example:

Chatbox say: 
/givemoney Shido 1 
  
Output: 
* Shido gave Shido $1 
--------------------------- 
Chatbox say: 
!givemoney Shido 1 
  
Output: 
Shido: !givemoney Shido 1 
* Shido gave Shido $1 
  

The best way I could think was making the server detect when the player uses the "!" at the start of the message and putting all the words on a table, then using the entries of the table to make an executeCommandHandler

So after a little working I got this:

addEventHandler("onPlayerChat",root,function(msg,msgtype) 
    if msgtype == 0 then 
        local s,e = string.find(msg,"%!") -- Finding the "!" char 
        if s == 1 then 
            local cmdmsg = string.gsub(msg,"!","",1) -- Extracting the "!" char from the string 
            local args = string.explode(cmdmsg," ") -- Function from the "Useful Functions" page on the wiki 
            executeCommandHandler(args[1],source,args[2],args[3],etc...) -- Finding a way to put all the arguments...... 
        end 
    end 
end) 

Is there a best way to get this working? or if i'm in the right way, is there an appropiate way to put the arguments on the execute function? (because i know the args[2],args[3],etc... is a really bad way)

Link to comment
  addEventHandler("onPlayerChat",root,function(msg,msgtype) 
            if msgtype == 0 then 
                local s,e = string.find(msg,"%!") 
                if s == 1 then 
                    local cmdmsg = string.gsub(msg,"!","",1) 
                    local args = string.explode(cmdmsg," ") 
                    local arguments = 1 
                    for i,v in pairs(args) do 
                    if i > 1 then 
                    local arga = arguments 
                    if arga == 1 then 
                    local arguments = v  
                    else 
                    local arguments = arga..","..v  
                    end 
                    end 
                    end 
                    executeCommandHandler(args[1],source,arguments) 
                end 
            end 
        end) 
         

Not tested what so ever but in theory it should work maybe.

Link to comment
      addEventHandler("onPlayerChat",root,function(msg,msgtype) 
                if msgtype == 0 then 
                    local s,e = string.find(msg,"%!") 
                    if s == 1 then 
                        local cmdmsg = string.gsub(msg,"!","",1) 
                        local args = string.explode(cmdmsg," ") 
                        local arguments = 1 
                        for i,v in pairs(args) do 
                        if i > 1 then 
                        local arga = arguments 
                        if arga == 1 then 
                        local arguments = v 
                        else 
                        local arguments = arga..,..v  -- Well?  
                        end 
                        end 
                        end 
                        executeCommandHandler(args[1],source,arguments) 
                    end 
                end 
            end) 
             

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...