• 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!

Solved attempt to concatenate local 'x' (a table value) -- npc issue

Xikini

I whore myself out for likes
Senator
Joined
Nov 17, 2010
Messages
6,812
Solutions
582
Reaction score
5,372
0.3.7

Hey everyone.
Trying to create a task npc.
Instead of writing out each task separately, I thought it would be a good idea to just stuff them into a table, and make it nice and generic, and learn a thing or two in the process.

Well I'm here, because I'm apparently not learning fast enough. :p

Whenever I call the value x by itself, it works, however if I try to play around with it at all.. well it just screams at me.

This works.
Code:
elseif x  then
     selfSay("Would like you like to start the task?", cid)
This works.
Code:
elseif x and getPlayerStorageValue(cid, x.storage) < 1 then
     selfSay("Would like you like to start the task?", cid)
This does not work, and pulls the error below
Code:
  elseif x and getPlayerStorageValue(cid, x.storage) < 1 then
     selfSay("Would like you like to start the ".. x .." task?", cid)
--------------------------------------------------------------------------------------------------------------------------------------------------
cIntqLt.png

--------------------------------------------------------------------------------------------------------------------------------------------------
Can someone point me in the right direction, or show me what I'm doing wrong?
Thanks!

Sorry for the tables, I promise they look better with real tabs :p
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local xmsg = {}

function onCreatureAppear(cid)  npcHandler:onCreatureAppear(cid)  end
function onCreatureDisappear(cid)  npcHandler:onCreatureDisappear(cid)  end
function onCreatureSay(cid, type, msg)  npcHandler:onCreatureSay(cid, type, msg)  end
function onThink()  npcHandler:onThink()  end

function greet(cid)
     talkState[cid] = 0
     return true
end

function getNpcName()
     return getCreatureName(getNpcId())
end

function creatureSayCallback(cid, type, msg)
     if(not npcHandler:isFocused(cid)) then
       return false
     end

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

     local task = {
         [1]    = {thing = "wasps",        storageID = 45003, valueMin = -1, valueMax = 1},
         [2]    = {thing = "trolls",        storageID = 45005, valueMin = -1, valueMax = 1},
         [3]    = {thing = "goblins",        storageID = 45007, valueMin = -1, valueMax = 1},
         [4]    = {thing = "slimes",        storageID = 45009, valueMin = -1, valueMax = 1},
         [5]    = {thing = "rotworms",      storageID = 45011, valueMin = -1, valueMax = 1},
         [6]    = {thing = "amazons",        storageID = 45013, valueMin = -1, valueMax = 1},
         [7]    = {thing = "valktries",      storageID = 45015, valueMin = -1, valueMax = 1},
         [8]    = {thing = "larva",        storageID = 45017, valueMin = -1, valueMax = 1},
         [9]    = {thing = "orcs",        storageID = 45019, valueMin = -1, valueMax = 1},
         [10] = {thing = "dwarves",        storageID = 45021, valueMin = -1, valueMax = 1},
         [11] = {thing = "minotaurs",      storageID = 45023, valueMin = -1, valueMax = 1},
         [12] = {thing = "ghouls",        storageID = 45025, valueMin = -1, valueMax = 1},
         [13] = {thing = "scarabs",        storageID = 45027, valueMin = -1, valueMax = 1},
         [14] = {thing = "ancient scarabs",    storageID = 45029, valueMin = -1, valueMax = 1},
         [15] = {thing = "demons skeletons", storageID = 45031, valueMin = -1, valueMax = 1},
         [16] = {thing = "cyclops",        storageID = 45033, valueMin = -1, valueMax = 1},
         [17] = {thing = "vampires",      storageID = 45035, valueMin = -1, valueMax = 1},
         [18] = {thing = "necromancers",    storageID = 45037, valueMin = -1, valueMax = 1},
         [19] = {thing = "giant spiders",    storageID = 45039, valueMin = -1, valueMax = 1},
         [20] = {thing = "dragons",        storageID = 45041, valueMin = -1, valueMax = 1},
         [21] = {thing = "dragon lords",    storageID = 45043, valueMin = -1, valueMax = 1},
         [22] = {thing = "demons",        storageID = 45045, valueMin = -1, valueMax = 1}
     }

     local config = {
         ["wasps"]         = {storage = 45003, experience = 1000, reward = 2160, count = 1},
         ["trolls"]         = {storage = 45005, experience = 1000, reward = 2160, count = 1},
         ["goblins"]         = {storage = 45007, experience = 1000, reward = 2160, count = 1},
         ["slimes"]         = {storage = 45009, experience = 1000, reward = 2160, count = 1},
         ["rotworms"]       = {storage = 45011, experience = 1000, reward = 2160, count = 1},
         ["amazons"]         = {storage = 45013, experience = 1000, reward = 2160, count = 1},
         ["valktries"]       = {storage = 45015, experience = 1000, reward = 2160, count = 1},
         ["larva"]         = {storage = 45017, experience = 1000, reward = 2160, count = 1},
         ["orcs"]         = {storage = 45019, experience = 1000, reward = 2160, count = 1},
         ["dwarves"]         = {storage = 45021, experience = 1000, reward = 2160, count = 1},
         ["minotaurs"]       = {storage = 45023, experience = 1000, reward = 2160, count = 1},
         ["ghouls"]         = {storage = 45025, experience = 1000, reward = 2160, count = 1},
         ["scarabs"]         = {storage = 45027, experience = 1000, reward = 2160, count = 1},
         ["ancient scarabs"]     = {storage = 45029, experience = 1000, reward = 2160, count = 1},
         ["demons skeletons"]   = {storage = 45031, experience = 1000, reward = 2160, count = 1},
         ["cyclops"]         = {storage = 45033, experience = 1000, reward = 2160, count = 1},
         ["vampires"]       = {storage = 45035, experience = 1000, reward = 2160, count = 1},
         ["necromancers"]     = {storage = 45037, experience = 1000, reward = 2160, count = 1},
         ["giant spiders"]     = {storage = 45039, experience = 1000, reward = 2160, count = 1},
         ["dragons"]         = {storage = 45041, experience = 1000, reward = 2160, count = 1},
         ["dragon lords"]     = {storage = 45043, experience = 1000, reward = 2160, count = 1},
         ["demons"]         = {storage = 45045, experience = 1000, reward = 2160, count = 1}
     }

     local x = config[msg:lower()]

     if msgcontains(msg, "task") then
         local npcmsg = ""
         for i = 1, #task do
             local storage = getPlayerStorageValue(cid, task[i].storageID)
             if storage <= task[i].valueMax and storage >= task[i].valueMin then
                 if npcmsg ~= "" then
                     npcmsg = npcmsg.. ", "
                 end
                 npcmsg = npcmsg..task[i].thing
             end
         end
         selfSay("Tasks available: " ..npcmsg.. ".", cid)

     elseif x then
         if getPlayerStorageValue(cid, x.storage) < 1 then
             selfSay("Would like you like to start the ".. x .." task?", cid)
             talkState[talkUser] = 1
             xmsg[cid] = msg
         elseif getPlayerStorageValue(cid, x.storage) == 1 then
             selfSay("Have you completed your task?", cid)
             talkState[talkUser] = 2
             xmsg[cid] = msg
         elseif getPlayerStorageValue(cid, x.storage) > 1 then
             selfSay("You have already completed this task. Thank you.", cid)
         end

     elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
         local x = config[xmsg[cid]:lower()]
         selfSay("Great! Once you have killed 50 ".. x .." come back and see me.", cid)
         setPlayerStorageValue(cid, x.storage, 1)
         talkState[talkUser] = 0
  
     elseif msgcontains(msg, "yes") and talkState[talkUser] == 2 then
         local x = config[xmsg[cid]:lower()]
         if getPlayerStorageValue(cid, x.storage + 1) == 30 then
             selfSay("Wonderful! Here is your reward.", cid)
             setPlayerStorageValue(cid, x.storage, 2)
             doPlayerAddExperience(cid, x.experience)
             doPlayerAddItem(cid, x.reward, x.count, true)
         else
             selfSay("You have not killed enough ".. x ..". Come back when you have killed more.", cid)
         end
         talkState[talkUser] = 0
    
     elseif msgcontains(msg, "reset") then
         setPlayerStorageValue(cid, 45003, -1)
         setPlayerStorageValue(cid, 45004, -1)
     end

     return true
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
x is a table, not a string, you are trying to print it as if it were a string
"Would like you like to start the ".. x .." task?"

Code:
con·cat·e·nate
/kənˈkatnˌāt/
verb formal technical
link (things) together in a chain or series.
"some words may be concatenated, such that certain sounds are omitted"
 
0.3.7

Hey everyone.
Trying to create a task npc.
Instead of writing out each task separately, I thought it would be a good idea to just stuff them into a table, and make it nice and generic, and learn a thing or two in the process.

Well I'm here, because I'm apparently not learning fast enough. :p

Whenever I call the value x by itself, it works, however if I try to play around with it at all.. well it just screams at me.

This works.
Code:
elseif x  then
     selfSay("Would like you like to start the task?", cid)
This works.
Code:
elseif x and getPlayerStorageValue(cid, x.storage) < 1 then
     selfSay("Would like you like to start the task?", cid)
This does not work, and pulls the error below
Code:
  elseif x and getPlayerStorageValue(cid, x.storage) < 1 then
     selfSay("Would like you like to start the ".. x .." task?", cid)
--------------------------------------------------------------------------------------------------------------------------------------------------
cIntqLt.png

--------------------------------------------------------------------------------------------------------------------------------------------------
Can someone point me in the right direction, or show me what I'm doing wrong?
Thanks!

Sorry for the tables, I promise they look better with real tabs :p
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}
local xmsg = {}

function onCreatureAppear(cid)  npcHandler:onCreatureAppear(cid)  end
function onCreatureDisappear(cid)  npcHandler:onCreatureDisappear(cid)  end
function onCreatureSay(cid, type, msg)  npcHandler:onCreatureSay(cid, type, msg)  end
function onThink()  npcHandler:onThink()  end

function greet(cid)
     talkState[cid] = 0
     return true
end

function getNpcName()
     return getCreatureName(getNpcId())
end

function creatureSayCallback(cid, type, msg)
     if(not npcHandler:isFocused(cid)) then
       return false
     end

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

     local task = {
         [1]    = {thing = "wasps",        storageID = 45003, valueMin = -1, valueMax = 1},
         [2]    = {thing = "trolls",        storageID = 45005, valueMin = -1, valueMax = 1},
         [3]    = {thing = "goblins",        storageID = 45007, valueMin = -1, valueMax = 1},
         [4]    = {thing = "slimes",        storageID = 45009, valueMin = -1, valueMax = 1},
         [5]    = {thing = "rotworms",      storageID = 45011, valueMin = -1, valueMax = 1},
         [6]    = {thing = "amazons",        storageID = 45013, valueMin = -1, valueMax = 1},
         [7]    = {thing = "valktries",      storageID = 45015, valueMin = -1, valueMax = 1},
         [8]    = {thing = "larva",        storageID = 45017, valueMin = -1, valueMax = 1},
         [9]    = {thing = "orcs",        storageID = 45019, valueMin = -1, valueMax = 1},
         [10] = {thing = "dwarves",        storageID = 45021, valueMin = -1, valueMax = 1},
         [11] = {thing = "minotaurs",      storageID = 45023, valueMin = -1, valueMax = 1},
         [12] = {thing = "ghouls",        storageID = 45025, valueMin = -1, valueMax = 1},
         [13] = {thing = "scarabs",        storageID = 45027, valueMin = -1, valueMax = 1},
         [14] = {thing = "ancient scarabs",    storageID = 45029, valueMin = -1, valueMax = 1},
         [15] = {thing = "demons skeletons", storageID = 45031, valueMin = -1, valueMax = 1},
         [16] = {thing = "cyclops",        storageID = 45033, valueMin = -1, valueMax = 1},
         [17] = {thing = "vampires",      storageID = 45035, valueMin = -1, valueMax = 1},
         [18] = {thing = "necromancers",    storageID = 45037, valueMin = -1, valueMax = 1},
         [19] = {thing = "giant spiders",    storageID = 45039, valueMin = -1, valueMax = 1},
         [20] = {thing = "dragons",        storageID = 45041, valueMin = -1, valueMax = 1},
         [21] = {thing = "dragon lords",    storageID = 45043, valueMin = -1, valueMax = 1},
         [22] = {thing = "demons",        storageID = 45045, valueMin = -1, valueMax = 1}
     }

     local config = {
         ["wasps"]         = {storage = 45003, experience = 1000, reward = 2160, count = 1},
         ["trolls"]         = {storage = 45005, experience = 1000, reward = 2160, count = 1},
         ["goblins"]         = {storage = 45007, experience = 1000, reward = 2160, count = 1},
         ["slimes"]         = {storage = 45009, experience = 1000, reward = 2160, count = 1},
         ["rotworms"]       = {storage = 45011, experience = 1000, reward = 2160, count = 1},
         ["amazons"]         = {storage = 45013, experience = 1000, reward = 2160, count = 1},
         ["valktries"]       = {storage = 45015, experience = 1000, reward = 2160, count = 1},
         ["larva"]         = {storage = 45017, experience = 1000, reward = 2160, count = 1},
         ["orcs"]         = {storage = 45019, experience = 1000, reward = 2160, count = 1},
         ["dwarves"]         = {storage = 45021, experience = 1000, reward = 2160, count = 1},
         ["minotaurs"]       = {storage = 45023, experience = 1000, reward = 2160, count = 1},
         ["ghouls"]         = {storage = 45025, experience = 1000, reward = 2160, count = 1},
         ["scarabs"]         = {storage = 45027, experience = 1000, reward = 2160, count = 1},
         ["ancient scarabs"]     = {storage = 45029, experience = 1000, reward = 2160, count = 1},
         ["demons skeletons"]   = {storage = 45031, experience = 1000, reward = 2160, count = 1},
         ["cyclops"]         = {storage = 45033, experience = 1000, reward = 2160, count = 1},
         ["vampires"]       = {storage = 45035, experience = 1000, reward = 2160, count = 1},
         ["necromancers"]     = {storage = 45037, experience = 1000, reward = 2160, count = 1},
         ["giant spiders"]     = {storage = 45039, experience = 1000, reward = 2160, count = 1},
         ["dragons"]         = {storage = 45041, experience = 1000, reward = 2160, count = 1},
         ["dragon lords"]     = {storage = 45043, experience = 1000, reward = 2160, count = 1},
         ["demons"]         = {storage = 45045, experience = 1000, reward = 2160, count = 1}
     }

     local x = config[msg:lower()]

     if msgcontains(msg, "task") then
         local npcmsg = ""
         for i = 1, #task do
             local storage = getPlayerStorageValue(cid, task[i].storageID)
             if storage <= task[i].valueMax and storage >= task[i].valueMin then
                 if npcmsg ~= "" then
                     npcmsg = npcmsg.. ", "
                 end
                 npcmsg = npcmsg..task[i].thing
             end
         end
         selfSay("Tasks available: " ..npcmsg.. ".", cid)

     elseif x then
         if getPlayerStorageValue(cid, x.storage) < 1 then
             selfSay("Would like you like to start the ".. x .." task?", cid)
             talkState[talkUser] = 1
             xmsg[cid] = msg
         elseif getPlayerStorageValue(cid, x.storage) == 1 then
             selfSay("Have you completed your task?", cid)
             talkState[talkUser] = 2
             xmsg[cid] = msg
         elseif getPlayerStorageValue(cid, x.storage) > 1 then
             selfSay("You have already completed this task. Thank you.", cid)
         end

     elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
         local x = config[xmsg[cid]:lower()]
         selfSay("Great! Once you have killed 50 ".. x .." come back and see me.", cid)
         setPlayerStorageValue(cid, x.storage, 1)
         talkState[talkUser] = 0
 
     elseif msgcontains(msg, "yes") and talkState[talkUser] == 2 then
         local x = config[xmsg[cid]:lower()]
         if getPlayerStorageValue(cid, x.storage + 1) == 30 then
             selfSay("Wonderful! Here is your reward.", cid)
             setPlayerStorageValue(cid, x.storage, 2)
             doPlayerAddExperience(cid, x.experience)
             doPlayerAddItem(cid, x.reward, x.count, true)
         else
             selfSay("You have not killed enough ".. x ..". Come back when you have killed more.", cid)
         end
         talkState[talkUser] = 0
   
     elseif msgcontains(msg, "reset") then
         setPlayerStorageValue(cid, 45003, -1)
         setPlayerStorageValue(cid, 45004, -1)
     end

     return true
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
local x = config[msg:lower()]

x is a table, you cant concatenate it with a string. You can use msg:lower() instead of x in this msg, or you can add another index to the table to store its name, like:
{name = "demons", storage = 45045, experience = 1000, reward = 2160, count = 1}

And then you can use x.name.
 
Use msg,
"Would like you like to start the ".. msg:lower() .." task?"
or
"Would like you like to start the ".. msg .." task?"
 
Kind of off topic.. but how can I print the string?
Code:
  local config = {
     ["wasps"] = {storage = 45003, count = 50, experience = 1000, reward = 2160, count = 1}
   }
If I try
Code:
print(config[1]
print(config[lower())
print(config[[1]lower())
They always come back as nil.. which makes sense, because I have no idea how to call the value. :p
 
Code:
config[1] returns nil,
config[lower()] throws an error "attempt to call global 'lower' (a nil value)" because it is a metamethod of string
config[[1]lower() throws an error "unfinished long string near '<eof>'"
The last thing you tried to print is a clerical error, however it still would not work as there is no index 1

This is why it is important to learn the core language so that you have a better understanding on how to access/set data.

I don't understand why you would want to print the index value of the table when you are using a value to access the index of the table, just use that value.
Doesn't mean that it can not be done, it just seems like over kill to create a function to do that.
 
Code:
config[1] returns nil,
config[lower()] throws an error "attempt to call global 'lower' (a nil value)" because it is a metamethod of string
config[[1]lower() throws an error "unfinished long string near '<eof>'"
The last thing you tried to print is a clerical error, however it still would not work as there is no index 1

This is why it is important to learn the core language so that you have a better understanding on how to access/set data.

I don't understand why you would want to print the index value of the table when you are using a value to access the index of the table, just use that value.
Doesn't mean that it can not be done, it just seems like over kill to create a function to do that.
Because I wanted to avoid doing this.
Code:
local config = {
["wasps"] = {name = "wasps", storage = 45003, count = 50, experience = 1000, reward = 2160, count = 1}
}

-- Edit..
Not that that worked either. Lol.
Oh well, I'll figure something out.
 
Last edited:
Because I wanted to avoid doing this.
Code:
local config = {
["wasps"] = {name = "wasps", storage = 45003, count = 50, experience = 1000, reward = 2160, count = 1}
}

-- Edit..
Not that that worked either. Lol.
Oh well, I'll figure something out.
Just use this function, I am assuming you want to list all the tasks from the table?
Code:
function getTasks(t)
    local x = {}
    for k, v in pairs(t) do
        table.insert(x, k..', ')
    end
    local s = table.concat(x)
    return s:sub(1, #s - 2)..' '
end
By passing the config table as an argument to the above function, this is returned as the result.
Code:
ghouls, demons, slimes, cyclops, goblins, dwarves, ancient scarabs, orcs, dragons, larva, vampires, wasps, amazons, necromancers, rotworms, dragon lords, valktries, demons skeletons, giant spiders, scarabs, minotaurs, trolls
 
Last edited:
Back
Top