Jump to content

Table


-.Paradox.-

Recommended Posts

Hello, Is this possible?

e.i: /command id2 outputs "Hello" and if inserted /command id1 it will output "Hello World", Thanks in advance.

The code is a little bit messy, sorry about it

local table = {"Hello World", "id1", "path2"}, 
    {"Hello", "id2", "path2"}, 
} 
  
function infocmd(me, _, info) 
local info = tostring(info) 
    if info == table[id] then 
        --code 
        outputChatBox(--[[idk what to insert here to make it output first text on the table]]) 
    end 
end 
addCommandHandler("command", infocmd) 

Link to comment

At first, never create a table called "table" because this will erase all your functions like table.insert from your current Lua VM (serverside scripts, in your case). Outputting 1st argument depending on the 2nd is pretty easy. Or, do you want to specify the id of the message instead of its argument? First example (reading argument from the table)

local tbl = { 
{"Hello World", "id1", "path2"}, 
{"Hello", "id2", "path2"}, 
} 
  
function infocmd(me, _,info) 
  local info = tostring(info) 
  for i,v in ipairs(tbl) do -- yep, only looping to find it 
    if v[2] == info then -- check if there is such value2 in the table 
      outputChatBox(v[1]) -- output "Hello World" of command had"id1", "Hello" if command had "id2" 
      return -- ending the function when we found it, who needs to loop through useless data anyway 
    end 
  end 
end 
addCommandHandler("command", infocmd) 

But if you want to use the id itself (/command 5), you can make it like this:

local tbl = { 
{"Hello World","path2"}, -- you don't need ids here anyway 
{"Hello","path2"}, 
} 
  
function infocmd(me, _,info) 
  local info = tonumber(info) 
  if not info then return end -- if it's not a number, then end the function 
  local val = tbl[info] -- get table value with your specified index 
  if val and val[1] then -- if the table number exists and there is a message in it 
    outputChatBox(val[1]) -- output "Hello World" of command had "1", output "Hello" if it had 2 
  end 
end 
addCommandHandler("command", infocmd) 

Link to comment

One little question, What if i wanted to output in chat box a random text from second variable, like:

local tbl = {"It won't output this one", "But it will output this one"}, 
{"It won't output this one1", "But it will output this one1"}, 
{"It won't output this one2", "But it will output this one2"}, 
} 
local text1,text2 = unpack(tbl) 
outputChatBox(tostring(math.random(text2)), 255,55,55,false) --? 

Thanks in advance

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