• 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 [TFS 0.X] Info Command - Error with specifics monster

potinho

Advanced OT User
Joined
Oct 11, 2009
Messages
1,402
Solutions
17
Reaction score
150
Location
Brazil
I have this script to show monster life, exp and loots:

Lua:
function ExistMonsterByName(name) -- by vodka
    local monster = io.open("data/monster/monsters.xml", "r"):read("*all")
    local get = monster:lower():match('name="' .. name:lower() ..'"')
    if get == nil or get == "" then
        return false
    end
    return true
end
function getDirMonsterByNameMonster(name)
    local t = {}
    local monster = io.open("data/monster/monsters.xml", "r")
    for i in monster:read("*a"):gmatch('<monster name="'..tostring(name)..'" file="(.-)"/>') do
        table.insert(t, tostring(i))
    end
    return t[1] or 0
end
function getMonsterLootItens(name)
    local dir = "data/monster/"..getDirMonsterByNameMonster(name)..""
    local monster = io.open(""..dir.."", "r")
    str = ""
    for i in monster:read("*a"):gmatch('id="(.-)"') do
        str = ""..str.." - "..getItemNameById(i)..""
    end
    return str
end
function getAllMonster()
    local str = ""
    local monster = io.open("data/monster/monsters.xml", "r")
    str = "Voce digitou incorretamente o nome do monstro veja a lista de monstro\n"
    for i in monster:read("*a"):gmatch('<monster name="(.-)"') do
        str = ""..str.." - "..i..""
    end
    return str
end
function getAttrMonster(name)
    return "Life = "..getMonsterInfo(name).health.."\nExp = "..getMonsterInfo(name).experience.."\n"
end
function onSay(cid, words, param, channel)
    if param == "" or not param or param == " " then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "You need to enter the monster's name.")
        return true
    end
    local name = param:lower():gsub("%p", "")
    if not ExistMonsterByName(name) then 
        doShowTextDialog(cid, 1397, getAllMonster()) return true
    end
    local name = name:gsub("(%l)(%w*)", function(a,b) return string.upper(a)..b end)
    doShowTextDialog(cid, 1397, "Info Monster "..name.."\n"..getAttrMonster(name).."\n\nLoots = "..getMonsterLootItens(name).."")
    return true
end

But give a error with specific monster, like "Elder Beholder", this monster exists in server. Follow error:

2022-07-06 17:19:05 - [Error - TalkAction Interface]
2022-07-06 17:19:05 - data/talkactions/scripts/info.lua:eek:nSay
2022-07-06 17:19:05 - Description:
2022-07-06 17:19:05 - data/lib/100-shortcut.lua:260: attempt to index a boolean value
2022-07-06 17:19:05 - stack traceback:
2022-07-06 17:19:05 - data/lib/100-shortcut.lua:260: in function 'getItemNameById'
2022-07-06 17:19:05 - data/talkactions/scripts/info.lua:22: in function 'getMonsterLootItens'
2022-07-06 17:19:05 - data/talkactions/scripts/info.lua:48: in function <data/talkactions/scripts/info.lua:38>


Can u guys help me to fix?
 
happening with some monsters like:

war wolf
XML:
<?xml version="1.0" encoding="UTF-8"?>
<monster name="War Wolf" nameDescription="a war wolf" race="blood" experience="55" speed="200" manacost="0">
    <health now="140" max="140"/>
    <look type="3" corpse="2969"/>
    <targetchange interval="2000" chance="0"/>
    <strategy attack="100" defense="0"/>
    <flags>
        <flag summonable="0"/>
        <flag attackable="1"/>
        <flag hostile="1"/>
        <flag illusionable="1"/>
        <flag convinceable="1"/>
        <flag pushable="0"/>
        <flag canpushitems="1"/>
        <flag canpushcreatures="0"/>
        <flag targetdistance="1"/>
        <flag staticattack="90"/>
        <flag runonhealth="0"/>
    </flags>
    <attacks>
        <attack name="melee" interval="2000" skill="40" attack="20"/>
    </attacks>
    <defenses armor="10" defense="16"/>
    <elements>
        <element icePercent="-10"/>
    </elements>
    <voices interval="5000" chance="10">
        <voice sentence="Grrrrrrr"/>
        <voice sentence="Yoooohhuuuu!" yell="1"/>
    </voices>
    <loot>
        <item id="2666" countmax="4" chance="25000"/><!-- meat -->
        <item id="2671" countmax="2" chance="20000"/><!-- ham -->
    </loot>
</monster>

reflection of mawhawk
XML:
<?xml version="1.0" encoding="UTF-8"?>
<monster name="Reflection of Mawhawk" nameDescription="a reflection of mawhawk" race="blood" experience="6400" speed="780" manacost="0">
    <health now="6400" max="6400"/>
    <look type="464" corpse="5676"/>
    <targetchange interval="5000" chance="8"/>
    <strategy attack="100" defense="0"/>
    <flags>
        <flag summonable="0"/>
        <flag attackable="1"/>
        <flag hostile="1"/>
        <flag illusionable="0"/>
        <flag convinceable="0"/>
        <flag pushable="0"/>
        <flag canpushitems="1"/>
        <flag canpushcreatures="1"/>
        <flag targetdistance="1"/>
        <flag staticattack="90"/>
        <flag runonhealth="0"/>
    </flags>
    <attacks>
        <attack name="melee" interval="2000" skill="145" attack="110"/>
            <attack name="physical" interval="1000" chance="60" range="7" min="-1000" max="-2500">
            <attribute key="shootEffect" value="largerock"/>
        </attack>
    </attacks>
    <defenses armor="100" defense="180"/>
    <elements>
        <element firePercent="-10"/>
        <element physicalPercent="50"/>
    </elements>
    <immunities>
        <immunity paralyze="1"/>
        <immunity invisible="1"/>
    </immunities>
    <loot>
        <item id="2148" countmax="15" chance="9000"/><!-- gold coin -->
        <item id="1294" countmax="4" chance="8250"/><!-- small stone -->
        <item id="2509" chance="9750"/><!-- steel shield -->
        <item id="2050" chance="7250"/><!-- torch -->
        <item id="2166" chance="5000"/><!-- power ring-->
        <item id="2483" chance="3250"/><!-- scale armor -->
        <item id="2395" chance="1100"/><!-- carlin sword -->
        <item id="2124" chance="200"/><!-- crystal ring -->
        <item id="2156" chance="150"/><!-- red gem -->
    </loot>
</monster>

Error:
2022-07-14 09:27:01 - [Error - TalkAction Interface]
2022-07-14 09:27:01 - data/talkactions/scripts/info.lua:eek:nSay
2022-07-14 09:27:01 - Description:
2022-07-14 09:27:01 - data/talkactions/scripts/info.lua:21: attempt to index local 'monster' (a nil value)
2022-07-14 09:27:01 - stack traceback:
2022-07-14 09:27:01 - data/talkactions/scripts/info.lua:21: in function 'getMonsterLootItens'
2022-07-14 09:27:01 - data/talkactions/scripts/info.lua:48: in function <data/talkactions/scripts/info.lua:38>
 
I think it means that item with given ID does not exist.
Replace:
Code:
str = ""..str.." - "..getItemNameById(i)..""
with:
Code:
print('trying to load item:', i, type(i))
str = ""..str.." - "..getItemNameById(i)..""
and check what it shows above error in console. It should show IDs loaded by script and last ID will be ID that does not exist.

Anyway, why don't you use function getMonsterInfo instead of reading XML in Lua?
Lua:
local monsterInfo = getMonsterInfo(name)
if monsterInfo then
   for k, v in pairs(monsterInfo.loot) do
      print('loot', name, v.id, v.count, v.chance)
   end
else
   print('monster not found')
end
 
I think it means that item with given ID does not exist.
Replace:
Code:
str = ""..str.." - "..getItemNameById(i)..""
with:
Code:
print('trying to load item:', i, type(i))
str = ""..str.." - "..getItemNameById(i)..""
and check what it shows above error in console. It should show IDs loaded by script and last ID will be ID that does not exist.

Anyway, why don't you use function getMonsterInfo instead of reading XML in Lua?
Lua:
local monsterInfo = getMonsterInfo(name)
if monsterInfo then
   for k, v in pairs(monsterInfo.loot) do
      print('loot', name, v.id, v.count, v.chance)
   end
else
   print('monster not found')
end
I dont know why dont use, i get this code in this way, will try. Which function i have to replace @Gesior.pl ? Can u show me how to optimize code?
Post automatically merged:

Tried with another monster: War Wolf. Put like this:

Lua:
function getMonsterLootItens(name)
    local dir = "data/monster/"..getDirMonsterByNameMonster(name)..""
    local monster = io.open(""..dir.."", "r")
    str = ""
    for i in monster:read("*a"):gmatch('id="(.-)"') do
        print('trying to load item:', i, type(i))
        str = ""..str.." - "..getItemNameById(i)..""
    end
    return str
end
Got this error

2022-07-15 12:33:20 - [Error - TalkAction Interface]
2022-07-15 12:33:20 - data/talkactions/scripts/info.lua:eek:nSay
2022-07-15 12:33:20 - Description:
2022-07-15 12:33:20 - data/talkactions/scripts/info.lua:21: attempt to index local 'monster' (a nil value)
2022-07-15 12:33:20 - stack traceback:
2022-07-15 12:33:20 - data/talkactions/scripts/info.lua:21: in function 'getMonsterLootItens'
2022-07-15 12:33:20 - data/talkactions/scripts/info.lua:49: in function <data/talkactions/scripts/info.lua:39>

War Wolf.xml (i have those 2 itens in items)
XML:
<?xml version="1.0" encoding="UTF-8"?>
<monster name="War Wolf" nameDescription="a war wolf" race="blood" experience="55" speed="200" manacost="0">
    <health now="140" max="140"/>
    <look type="3" corpse="2969"/>
    <targetchange interval="2000" chance="0"/>
    <strategy attack="100" defense="0"/>
    <flags>
        <flag summonable="0"/>
        <flag attackable="1"/>
        <flag hostile="1"/>
        <flag illusionable="1"/>
        <flag convinceable="1"/>
        <flag pushable="0"/>
        <flag canpushitems="1"/>
        <flag canpushcreatures="0"/>
        <flag targetdistance="1"/>
        <flag staticattack="90"/>
        <flag runonhealth="0"/>
    </flags>
    <attacks>
        <attack name="melee" interval="2000" skill="40" attack="20"/>
    </attacks>
    <defenses armor="10" defense="16"/>
    <elements>
        <element icePercent="-10"/>
    </elements>
    <voices interval="5000" chance="10">
        <voice sentence="Grrrrrrr"/>
        <voice sentence="Yoooohhuuuu!" yell="1"/>
    </voices>
    <loot>
        <item id="2666" countmax="4" chance="25000"/><!-- meat -->
        <item id="2671" countmax="2" chance="20000"/><!-- ham -->
    </loot>
</monster>
 
Last edited:
up
I think it means that item with given ID does not exist.
Replace:
Code:
str = ""..str.." - "..getItemNameById(i)..""
with:
Code:
print('trying to load item:', i, type(i))
str = ""..str.." - "..getItemNameById(i)..""
and check what it shows above error in console. It should show IDs loaded by script and last ID will be ID that does not exist.

Anyway, why don't you use function getMonsterInfo instead of reading XML in Lua?
Lua:
local monsterInfo = getMonsterInfo(name)
if monsterInfo then
   for k, v in pairs(monsterInfo.loot) do
      print('loot', name, v.id, v.count, v.chance)
   end
else
   print('monster not found')
end
 
Back
Top