• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Tables LUA

tuxico

Member
Joined
Aug 2, 2010
Messages
52
Solutions
1
Reaction score
7
I'm trying all day to learn about "Tables" in LUA, but it seems complicated or does not have enough content on nested tables ... come on

I've created a table that does not only contain a Key and Value, it has several variables as well, as is common in any pokemon server or whatever ...

The Table
Lua:
Table = {
["demon"] = 1515, Vida = 5000, Level = 80, and other variables...
["orc"] = 1516, Vida = 3000, Level = 40, and other variables...
["rat"] = 1517, Vida = 1000, Level = 20, and other variables...
}
I want to access the variables within the table, by name in [], which as far as I know, are Keys; Ie I want to get the values of a line, having only the ID, which in the monster would be 1515 ... already tried various forms and nothing, console ever return "Nil Value".
CODE EXAMPLE
Lua:
   NovaPB:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "It a Contain a " .. PokeLife .. ".")
    local vida = getLifeForPoke(Table, demon)
    player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Pokemon Life: " .. vida)

function getLifeForPoke(t, nomepokemon)
   for k,v in pairs(t) do
       if k == nomepokemon  then
       return k.Vida
       end
   end
end
 
You're separating the values with comma (,) - thats wrong, cause lua thinks its value after value. ["demon"] = 1515, Vida = 5000

If you want to make a nested table, you have to use {} one more time. Like this:
Lua:
Table = {
["demon"] = {x = 1515, Vida = 5000, Level = 80}, and other variables...
["orc"] = {x = 1516, Vida = 3000, Level = 40}, and other variables...
["rat"] = {x = 1517, Vida = 1000, Level = 20, and other variables...
}

Now you can refer to this table like this:
Lua:
print(Table.demon.x)
print(Table.orc.Level)
And so on.

Hope i helped!
 
Right, you should use
Lua:
Table["demon"].x
Table["demon"].Level
etc..

And your script should look like this:
Lua:
function getLifeForPoke(t, nomepokemon)
    for k, v in pairs(t) do
        if k == nomepokemon  then
            return v.Vida
        end
    end
end

Notice i changed k.Vida to v.Vida
 
Now i changed order of the table...
Lua:
PokemonDB = {
[1515] = {nome = "demon", Vida = 5000, Level = 80},
[1516] = {nome = "orc", Vida = 2000, Level = 80},
[1517] = {nome = "rat", Vida = 1000, Level = 80}
}

function onUse(player, item, fromPosition, itemEx, toPosition, isHotkey)
   local corpse = itemEx
   if (string.find(corpse:getName(), "slain")) then
   toPosition:sendMagicEffect(100)
   corpse:remove()
   item:remove(1)
   ----------------------------------
   if math.random(0,100) >= 10 then
   local PokeName = string.gsub(corpse:getName(), "(slain)", "")
   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Parabens! Voce capturou um Pokemon:" .. PokeName)
   local NovaPB = player:addItem(2122,1)
   NovaPB:setAttribute(ITEM_ATTRIBUTE_DESCRIPTION, "It a Contain a " .. PokeName .. ".")
   NovaPB:setCustomAttribute("Holding_ID", 1515)
   local id = NovaPB:getCustomAttribute("Holding_ID")
   --local nomepokemon = PegarNomeDaDB(PokemonDB, id)
   local HP_NewPoke = PokemonDB.Vida
   
   NovaPB:setCustomAttribute("HP", HP_NewPoke)
   local get = NovaPB:getCustomAttribute("HP")
   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Vida: " .. get)
   
   else
   player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Tente Novamente... ")
   return false
   end
end
end
 
Maybe i need um ipairs itherator?
Lua:
function IndexForDB(t, id)
   for i,k in ipairs(t) do
   if k == id then
   return i
   end
   end
end
 
Last edited:
I Solved... the attribute acessor [], is for KEY, nothing Index...
local PokemonDB = {
Lua:
[1515] =
{holding = 1515,
Vida = 5000,
Level = 80
},
[20] =
{holding = 1515,
Vida = 1000,
Level = 80
},
[40] =
{holding = 1515,
Vida = 5000,
Level = 80
},
[60] =
{holding = 1515,
Vida = 9000,
Level = 80
},
[80] =
{holding = 1515,
Vida = 80000,
Level = 80
},
}
local HP_NewPoke = PokemonDB[1515].Vida
WORKS!
 
The error was in double quotation marks, so I could see... thanks for help <3
I'm working on a TFS 1.3 pokemon ... I plan to release it to the community.
 
Back
Top