• 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 how use string to get different informations

silveralol

Advanced OT User
Joined
Mar 16, 2010
Messages
1,480
Solutions
9
Reaction score
212
hello folks, I'm trying make something, so
with a string I want get the informations for the stuff
string = rat, 40, 100, 100, 40
where:
rat = name
40 = lvl
100 = healthnow
100 = maxhealth
40 = loot bonus
how I can get these informations with that string ?
I try with this for exemple:
Code:
local separatorPos = strings:split(",", 4)
    if separatorPos ~= nil then
        name = (strings[1])
        level = (strings[2])
        health = (strings[3])
        maxHealth = (strings[4])
        lootBonus = (strings[5])
    end
    local text = "Name: " ..name.. "\nLevel: " ..level.. "\nHealth: "..health.."/" ..maxHealth.. "\nBonus: " ..lootBonus.. "."
I did not understood that 4 in the separatorPos line, so please someone give some help
 
Solution
still indexing the incorrect variable
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey) 
    local name, level, health, maxHealth, lootBonus = "", 1, 0, 0, 0 -- Default values
    local myStringData = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION)
    local myDataTable = myStringData:split(", ")
    name = myDataTable[1] or name
    level = tonumber(myDataTable[2]) or level
    health = tonumber(myDataTable[3]) or health
    maxHealth = tonumber(myDataTable[4]) or maxHealth
    lootBonus = tonumber(myDataTable[5]) or lootBonus
    local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", name, level, health, maxHealth, lootBonus)
    print(text)
    return true
end
hello folks, I'm trying make something, so
with a string I want get the informations for the stuff
string = rat, 40, 100, 100, 40
where:
rat = name
40 = lvl
100 = healthnow
100 = maxhealth
40 = loot bonus
how I can get these informations with that string ?
I try with this for exemple:
Code:
local separatorPos = strings:split(",", 4)
    if separatorPos ~= nil then
        name = (strings[1])
        level = (strings[2])
        health = (strings[3])
        maxHealth = (strings[4])
        lootBonus = (strings[5])
    end
    local text = "Name: " ..name.. "\nLevel: " ..level.. "\nHealth: "..health.."/" ..maxHealth.. "\nBonus: " ..lootBonus.. "."
I did not understood that 4 in the separatorPos line, so please someone give some help

Where is the "strings" declaration?
 
hello folks, I'm trying make something, so
with a string I want get the informations for the stuff
string = rat, 40, 100, 100, 40
where:
rat = name
40 = lvl
100 = healthnow
100 = maxhealth
40 = loot bonus
how I can get these informations with that string ?
I try with this for exemple:
Code:
local separatorPos = strings:split(",", 4)
    if separatorPos ~= nil then
        name = (strings[1])
        level = (strings[2])
        health = (strings[3])
        maxHealth = (strings[4])
        lootBonus = (strings[5])
    end
    local text = "Name: " ..name.. "\nLevel: " ..level.. "\nHealth: "..health.."/" ..maxHealth.. "\nBonus: " ..lootBonus.. "."
I did not understood that 4 in the separatorPos line, so please someone give some help
the 4 means nothing, it shouldn't even be there
you should be indexing separatorPos[x] to get the values
 
So its 'string' or 'strings'?
Also separator should be with a space here i guess, like: ", ".
string.split(string, ", ")
 
Why cant you just do:

Code:
local tmp_array = {name = 'Rat', level = 30, health = 100, maxHealth = 100, lootBonus =  40}

    name = tmp_array.name
    level = tmp_array.level
    health = tmp_array.health
    maxHealth = tmp_array.maxHealth
    lootBonus = tmp_array.lootBonus

You should probably explain exactly what your doing. Are you trying to make it so you can create a string table in-game to run through a script? Like a talkaction?
 
is above... I just not write on the "code tag"
but the string declaration is:
Code:
local string = "rat, 30, 100, 100, 40"

I don't know what you are trying to do, but it should work:

Lua:
  local name, level, health, maxHealth, lootBonus = "", 1, 0, 0, 0 -- Default values
  local myStringData = "Rat,40,100,100,40"
  local myDataTable = myStringData:split(",")
  local isEmpty = next(myDataTable) == nil
  if not isEmpty then
    name = myStringData[1]
    level = tonumber(myStringData[2]) or level
    health = tonumber(myStringData[3]) or health
    maxHealth = tonumber(myStringData[4]) or maxHealth
    lootBonus = tonumber(myStringData[5]) or lootBonus
  end
  local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", name, level, health, maxHealth, lootBonus)
  print(text)

But I think you need something better like:

Lua:
  local text = "Not found."
  local myStringData = "Rat,40,100,100,40"
  local myDataTable = myStringData:split(",")
  local isEmpty = next(myDataTable) == nil
  if not isEmpty then
    text = string.format("Name: %s\nLevel: %s\nHealth: %s\nMaxHealth: %s\nBonus: %s.", myStringData[1], myStringData[2], myStringData[3], myStringData[4], myStringData[5])
  end
  print(text)

Or:

Lua:
  local myDataTable = { name = "Rat", level = 40, health = 100, maxHealth = 100, lootBonus = 40 }
  local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", myDataTable.name, myDataTable.level, myDataTable.health, myDataTable.maxHealth, myDataTable.lootBonus)
  print(text)
 
Last edited:
Why cant you just do:

Code:
local tmp_array = {name = 'Rat', level = 30, health = 100, maxHealth = 100, lootBonus =  40}

    name = tmp_array.name
    level = tmp_array.level
    health = tmp_array.health
    maxHealth = tmp_array.maxHealth
    lootBonus = tmp_array.lootBonus

You should probably explain exactly what your doing. Are you trying to make it so you can create a string table in-game to run through a script? Like a talkaction?
is just a pet system, that I'll use a string to get the informations of the pet, so I can't use predefined variables.

I don't know what you are trying to do, but it should work:

Lua:
  local name, level, health, maxHealth, lootBonus = "", 1, 0, 0, 0 -- Default values
  local myStringData = "Rat,40,100,100,40"
  local myDataTable = myStringData:split(",")
  local isEmpty = next(myDataTable) == nil
  if not isEmpty then
    name = myStringData[1]
    level = tonumber(myStringData[2]) or level
    health = tonumber(myStringData[3]) or health
    maxHealth = tonumber(myStringData[4]) or maxHealth
    lootBonus = tonumber(myStringData[5]) or lootBonus
  end
  local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", name, level, health, maxHealth, lootBonus)
  print(text)

But I think you need something better like:

Lua:
  local text = "Not found."
  local myStringData = "Rat,40,100,100,40"
  local myDataTable = myStringData:split(",")
  local isEmpty = next(myDataTable) == nil
  if not isEmpty then
    text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", myStringData[1], tonumber(myStringData[2]) or level, tonumber(myStringData[3]) or health, tonumber(myStringData[4]) or maxHealth, tonumber(myStringData[5]) or lootBonus)
  end
  print(text)

Or:

Lua:
  local myDataTable = { name = "Rat", level = 40, health = 100, maxHealth = 100, lootBonus = 40 }
  local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", myDataTable.name, myDataTable.level, myDataTable.health, myDataTable.maxHealth, myDataTable.lootBonus)
  print(text)
it print:
Code:
Name: nil
Level: 1
Health: 0
MaxHealth: 0
Bonus: 0.
i'm trying with another way, some friend help me with
Code:
vector = {Rat, 30, 500, 1000, 40}
for x = 1, 4, 1 do
        io.write(vector[x], "\n")
end
it print the current vector, but I can't figure out how this for works
 
Last edited by a moderator:
it print:
Code:
Name: nil
Level: 1
Health: 0
MaxHealth: 0
Bonus: 0.
i'm trying with another way, some friend help me with
Code:
vector = {Rat, 30, 500, 1000, 40}
for x = 1, 4, 1 do
        io.write(vector[x], "\n")
end
it print the current vector, but I can't figure out how this for works

Where is the full script? '-'
 
Where is the full script? '-'
atm
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local name, level, health, maxHealth, lootBonus = "", 1, 0, 0, 0 -- Default values
    local myStringData = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION)
    local myDataTable = myStringData:split(", ")
    local isEmpty = next(myDataTable) == nil
    if not isEmpty then
        name = myStringData[1]
        level = tonumber(myStringData[2]) or level
        health = tonumber(myStringData[3]) or health
        maxHealth = tonumber(myStringData[4]) or maxHealth
        lootBonus = tonumber(myStringData[5]) or lootBonus
    end
    local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", name, level, health, maxHealth, lootBonus)
    print(text)
    return true
end
it print what I said
Code:
Name: nil
Level: 1
Health: 0
MaxHealth: 0
Bonus: 0.
but I guess that the problem is because vector ~= string :/
 
still indexing the incorrect variable
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey) 
    local name, level, health, maxHealth, lootBonus = "", 1, 0, 0, 0 -- Default values
    local myStringData = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION)
    local myDataTable = myStringData:split(", ")
    name = myDataTable[1] or name
    level = tonumber(myDataTable[2]) or level
    health = tonumber(myDataTable[3]) or health
    maxHealth = tonumber(myDataTable[4]) or maxHealth
    lootBonus = tonumber(myDataTable[5]) or lootBonus
    local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", name, level, health, maxHealth, lootBonus)
    print(text)
    return true
end
 
Solution
still indexing the incorrect variable
Lua:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local name, level, health, maxHealth, lootBonus = "", 1, 0, 0, 0 -- Default values
    local myStringData = item:getAttribute(ITEM_ATTRIBUTE_DESCRIPTION)
    local myDataTable = myStringData:split(", ")
    name = myDataTable[1] or name
    level = tonumber(myDataTable[2]) or level
    health = tonumber(myDataTable[3]) or health
    maxHealth = tonumber(myDataTable[4]) or maxHealth
    lootBonus = tonumber(myDataTable[5]) or lootBonus
    local text = string.format("Name: %s\nLevel: %d\nHealth: %d\nMaxHealth: %d\nBonus: %d.", name, level, health, maxHealth, lootBonus)
    print(text)
    return true
end

why you use myStringData if the splited text is in myDataTable
you really cute
 
I made a video to show how this shit works fine, ofc that had to do alot of stuff to make it more "complete" and is still missing something as when you logout need to save the summon health, but atm, I forget to do in the video but when you call back the pet it save its "health now"
 
Last edited:
Back
Top