• 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 1.3] How to get a value from a name table

E

Evil Puncker

Guest
I have the following code (using TFS 1.3), and what I'm trying to do is, when the player use !bestiary rat, it gets the value from the storage in the table, but right now I can't figure out the proper code:

Lua:
local monsters = {
    ["rat"] = 35001,
    ["troll"] = 35002,
    ["rotworm"] = 35003,
    ["dragon"] = 35004,
    ["dragon lord"] = 35005,
    ["demon"] = 35006,
}

local bestiary = TalkAction("!bestiary")

function bestiary.onSay(player, words, param)
    local t = string.explode(param, ",")
    local tmp = t[1]
    if t[2] then
        tmp = t[2]
    end

    if param == '' then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Usage: !bestiary monster name")
        return true
    end

    local killedMonsters = player:getStorageValue(t[1])
    if not killedMonsters then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have not killed any " .. t[1] .. " yet.")
        return true
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have already killed " .. killedMonsters .. " " .. t[1] .. "s.")
    end
    return true
end

bestiary:separator(" ")
bestiary:register()

PS: it is a revscriptsys
 
Solution
I have the following code (using TFS 1.3), and what I'm trying to do is, when the player use !bestiary rat, it gets the value from the storage in the table, but right now I can't figure out the proper code:

Lua:
local monsters = {
    ["rat"] = 35001,
    ["troll"] = 35002,
    ["rotworm"] = 35003,
    ["dragon"] = 35004,
    ["dragon lord"] = 35005,
    ["demon"] = 35006,
}

local bestiary = TalkAction("!bestiary")

function bestiary.onSay(player, words, param)
    local t = string.explode(param, ",")
    local tmp = t[1]
    if t[2] then
        tmp = t[2]
    end

    if param == '' then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Usage: !bestiary monster name")
        return true
    end

    local...
I have the following code (using TFS 1.3), and what I'm trying to do is, when the player use !bestiary rat, it gets the value from the storage in the table, but right now I can't figure out the proper code:

Lua:
local monsters = {
    ["rat"] = 35001,
    ["troll"] = 35002,
    ["rotworm"] = 35003,
    ["dragon"] = 35004,
    ["dragon lord"] = 35005,
    ["demon"] = 35006,
}

local bestiary = TalkAction("!bestiary")

function bestiary.onSay(player, words, param)
    local t = string.explode(param, ",")
    local tmp = t[1]
    if t[2] then
        tmp = t[2]
    end

    if param == '' then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Usage: !bestiary monster name")
        return true
    end

    local killedMonsters = player:getStorageValue(t[1])
    if not killedMonsters then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have not killed any " .. t[1] .. " yet.")
        return true
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have already killed " .. killedMonsters .. " " .. t[1] .. "s.")
    end
    return true
end

bestiary:separator(" ")
bestiary:register()

PS: it is a revscriptsys
Lua:
monsters[tmp]
 
Solution
thanks it worked, now two more questions:

1 - when I use only !bestiary it gives error on console (attempt to index local 't' (a nil value)) instead of executing line 19

2 - how to make the script only work with the names in the table? if the player write something else it will say that creature don't exist
 
thanks it worked, now two more questions:

1 - when I use only !bestiary it gives error on console (attempt to index local 't' (a nil value)) instead of executing line 19

2 - how to make the script only work with the names in the table? if the player write something else it will say that creature don't exist
Lua:
local target = Creature(param)
    if not target then
        player:sendCancelMessage("creature not found.")
        return false
    end
if not monsters[tmp] then
    player:sendCancelMessage("monster not found in table")
    return false
end
 
Last edited:
You don't even need a separator, you have 1 argument to the command.
Lua:
local monsters = {
    ["rat"] = 35001,
    ["troll"] = 35002,
    ["rotworm"] = 35003,
    ["dragon"] = 35004,
    ["dragon lord"] = 35005,
    ["demon"] = 35006,
}

local bestiary = TalkAction("!bestiary")

function bestiary.onSay(player, words, param)
    if param == '' then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Usage: !bestiary monster name")
        return true
    end

    local storageKey = monsters[param:lower()]

    local killedMonsters = player:getStorageValue(storageKey)
    if killedMonsters == -1 then
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have not killed any " .. param .. " yet.")
    else
        player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have already killed " .. killedMonsters .. " " .. param .. "s.")
    end
    return true
end

bestiary:separator(" ")
bestiary:register()
 
I need a little help, where i put

local monsters = {
["rat"] = 35001,
["troll"] = 35002,
["rotworm"] = 35003,
["dragon"] = 35004,
["dragon lord"] = 35005,
["demon"] = 35006,
}

local bestiary = TalkAction("!bestiary")

function bestiary.onSay(player, words, param)
if param == '' then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "Usage: !bestiary monster name")
return true
end

local storageKey = monsters[param:lower()]

local killedMonsters = player:getStorageValue(storageKey)
if killedMonsters == -1 then
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_BLUE, "You have not killed any " .. param .. " yet.")
else
player:sendTextMessage(MESSAGE_STATUS_CONSOLE_ORANGE, "You have already killed " .. killedMonsters .. " " .. param .. "s.")
end
return true
end

bestiary:separator(" ")
bestiary:register()
 
Back
Top