• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Convert one text-string to a table value

Mjmackan

Mapper ~ Writer
Joined
Jul 18, 2009
Messages
1,474
Solutions
17
Reaction score
194
Location
Sweden
I'm stuck, I need to convert this nameString to a table value, right now im getting error in console: "index field 'namestring' (a nil value).
When printing namestring however i get the correct value, but when trying to use it for localizing in a table it gives me nil val. I have google'd lots and tried several types of lua variables without success.

Code:
LUA:
effect = 181, rewards0 = {{2160, 2}}

local lvlList =
{75, 150, 250, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000}

for i = 2,#lvlList do
local textString = tostring(lvlList[i])
nameString = ("rewards" .. textString)
local localize = rareType.nameString[1][1]
 
Solution
That might work, however I do have other table's inside that table like name, type, effect etc. But I might be able to use your method either way.
However If we go back to the topic, is there no way to serialize that string to a table? Or whats the reason for it not being able to fully understand the "nameString = ("rewards" .. textString)" when It does print correctly?
This is how LUA works, if table.nameString does not work it is because nameString does not exist in that table, but if nameString is a string we can extract the value contained in the variable nameString using square brackets [] and then if we are looking for the correct index, which is the string contained in the variable...
If you don't provide the full script, hardly anyone will be able to help you directly.
We do not know what is rareType, but according to the error you left in the publication is because the table rareType does not have the index [1] [1] before you can read a table, make sure the data is actually a table.

You can not do this:
This produces the same error that you mentioned above.
LUA:
local t = {}
t[1][1] = "name"

Instead you should know that the correct way is this:
LUA:
local t = {}
t[1] = {}
t[1][1] = "name"

--- OR ---
local t = {
 [1] = { [1] = "name" }
}

---
local t = {}
t[1] = { [1] = "name" }
 
If you don't provide the full script, hardly anyone will be able to help you directly.
We do not know what is rareType, but according to the error you left in the publication is because the table rareType does not have the index [1] [1] before you can read a table, make sure the data is actually a table.

You can not do this:
This produces the same error that you mentioned above.
LUA:
local t = {}
t[1][1] = "name"

Instead you should know that the correct way is this:
LUA:
local t = {}
t[1] = {}
t[1][1] = "name"

--- OR ---
local t = {
 [1] = { [1] = "name" }
}

---
local t = {}
t[1] = { [1] = "name" }
Its an abnormal long script, that is unfinished, unfortunately many times I've been met with disrespect rather than understanding in this forum, therefor I minimalize the information, maybe a bit too much this time though. Sorry for the inconvience, thanks for your thought of help.

Here's the script:

LUA:
local difficulty = {
rare = {rewards0 = {{2152, math.random(10,30)}}, rewards75 = {{2152, math.random(40,50)}}, rewards150 = {{2152, math.random(60,80)}}, rewards250 = {2152, math.random(80,99)}, rewards400 = {{2160, 1}}, rewards600 = {{2160, 1}}, rewards800 = {{2160, math.random(1,3)}}, rewards1000 = {{2160, math.random(1,3)}}, rewards1200 = {{2160, math.random(2,3)}}, rewards1400 = {{2160, math.random(2,3)}}, rewards1600 = {{2160, math.random(3,5)}}, rewards1800 = {{2160, math.random(3,5)}}, rewards2000 = {{2160, math.random(3,6)}}}
}

local function rareSpawn(id, mob, pos, lvl, rareType)
    local myPlayer = Player(id)
    if not myPlayer then   
        return
    end
player = Player(id)

local lvlList =
{75, 150, 250, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000}
 for i = 2,#lvlList do
  if lvl <= lvlList[i] and lvl >= lvlList[i-1] or lvl <= lvlList[i] then
  print("we in boys")
    local nameString = nil
    local textString = tostring(lvlList[i])
    nameString = ("rewards" .. textString)
    local localize2 = rareType.rewards150[1][1]
    print(localize2)
    print(nameString)
    local localize = rareType.nameString[1][1]
    print(localize)

    
    print("hej")
    
    
    return
  end
  return
 end
end

local function effect(id, mob, pos, rareType)
    local myPlayer = Player(id)
    if not myPlayer then
        return
    end
doSendMagicEffect(pos, rareType.effect)
end

function onKill(player, target, lastHit)
local rareType = nil
rareType = difficulty.rare

local lvl = target:getMonsterLevel()
local mob = target:getName()
local pos = target:getPosition()
    
    addEvent(rareSpawn, 5700, player.uid, mob, pos, lvl, rareType)
    
    return true
end
 
use local localize = rareType[nameString][1][1] instead local localize = rareType.nameString[1][1]
That might work, however I do have other table's inside that table like name, type, effect etc. But I might be able to use your method either way.
However If we go back to the topic, is there no way to serialize that string to a table? Or whats the reason for it not being able to fully understand the "nameString = ("rewards" .. textString)" when It does print correctly?
 
That might work, however I do have other table's inside that table like name, type, effect etc. But I might be able to use your method either way.
However If we go back to the topic, is there no way to serialize that string to a table? Or whats the reason for it not being able to fully understand the "nameString = ("rewards" .. textString)" when It does print correctly?
table.something equals table["something"] in lua and dot way of indexing tables won't work with dynamic variables such as strings. Unless you do a loadstring magic, but I wouldn't recommend that.
You have to index like you would do by numbers, because simply your string (nameString) is a string and table.something is simple faster way to index table by string index.
 
That might work, however I do have other table's inside that table like name, type, effect etc. But I might be able to use your method either way.
However If we go back to the topic, is there no way to serialize that string to a table? Or whats the reason for it not being able to fully understand the "nameString = ("rewards" .. textString)" when It does print correctly?
This is how LUA works, if table.nameString does not work it is because nameString does not exist in that table, but if nameString is a string we can extract the value contained in the variable nameString using square brackets [] and then if we are looking for the correct index, which is the string contained in the variable nameString

I can't see where the index nameString, i see only -> rewards0 rewards75 ....
GIF 04-09-2021 05-01-11 a. m..gif
 
Solution
table.something equals table["something"] in lua and dot way of indexing tables won't work with dynamic variables such as strings. Unless you do a loadstring magic, but I wouldn't recommend that.
You have to index like you would do by numbers, because simply your string (nameString) is a string and table.something is simple faster way to index table by string index.

This is how LUA works, if table.nameString does not work it is because nameString does not exist in that table, but if nameString is a string we can extract the value contained in the variable nameString using square brackets [] and then if we are looking for the correct index, which is the string contained in the variable nameString

I can't see where the index nameString, i see only -> rewards0 rewards75 ....
View attachment 61798
You girls/guys rocks. Thanks so much for putting down time teaching this to me, means lots. I will defintely remember this. THANKS! <3
 
Back
Top