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

TFS 1.X+ function arguments expected near ':'

anderkrox

New Member
Joined
May 14, 2020
Messages
21
Reaction score
1
[Warning - NpcScript::NpcScript] Can not load script: Grutgroc Broadbeard.lua
data/npc/scripts/Grutgroc Broadbeard.lua:35: function arguments expected near ':'
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local tabela = {
        [2392] = {
            name = "Fire Sword",
            price = 10000,
            ingredients = {[5880] = 90, [5468] = 2, [2406] = 1},
            failChance = 10,
            timeDone = 2,
        }
    }

    for k, v in pairs(tabela) do
        if player:getId(k) then
            if player:getMoney(tabela.price) then
                if player:getItemCount(tabela.ingredients, tabela.ingredients) then
                    if math.random(1, 100) > tabela.failchance then
                        if os.mtime() >= tabela.timeDone then
                            player:removeMoney(tabela.price)
                            player:removeItem(tabela.ingredients, tabela.ingredients)
                            npcHandler:say("Here, your ".. tabela.name ..".", cid)
                        end
                    else
                        npcHandler:say("Sorry, your item broke.", cid)
                    end
                else
                    npcHandler:say("You don't have ".. tabela.ingredients:item:getName() ..".", cid)
                end
            else
                npcHandler:say("You don't have money.", cid)
            end
            break
        end
    end
end

npcHandler:addModule(FocusModule:new())
 
Solution
Lua:
local temp_key = {}
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local itemInfo = {
        ["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the...
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local tabela = {
        [2392] = {
            name = "Fire Sword",
            price = 10000,
            ingredients = {[5880] = 90, [5468] = 2, [2406] = 1},
            failChance = 10,
            timeDone = 2,
        }
    }

    for k, v in pairs(tabela) do
        if player:getId(k) then
            if player:getMoney(tabela.price) then
                if player:getItemCount(tabela.ingredients, tabela.ingredients) then
                    if math.random(1, 100) > tabela.failchance then
                        if os.mtime() >= tabela.timeDone then
                            player:removeMoney(tabela.price)
                            player:removeItem(tabela.ingredients, tabela.ingredients)
                            npcHandler:say("Here, your ".. tabela.name ..".", cid)
                        end
                    else
                        npcHandler:say("Sorry, your item broke.", cid)
                    end
                else
                    local msg = ""
                    for i = 1, #tabela.ingredients do
                        msg = msg.. ""..ItemType(tabela.ingredients[i]):getName()..""
                        if i < #tabela.ingredient then
                            msg = msg.. ", "
                        end
                    end
                    npcHandler:say("You don't have ".. msg ..".", cid)
                end
            else
                npcHandler:say("You don't have money.", cid)
            end
            break
        end
    end
end

npcHandler:addModule(FocusModule:new())
 
Lua Script Error: [Npc interface]
data/npc/scripts/Grutgroc Broadbeard.lua:eek:nCreatureSay
data/npc/scripts/Grutgroc Broadbeard.lua:34: attempt to index a nil value
stack traceback:
[C]: in function '__index'
data/npc/scripts/Grutgroc Broadbeard.lua:34: in function 'callback'
data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
data/npc/scripts/Grutgroc Broadbeard.lua:7: in function <data/npc/scripts/Grutgroc Broadbeard.lua:7>
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local tabela = {
        [2392] = { -- What is the item ID that will be manufactured?
            name = "Fire Sword", -- What is the name of the item that will be manufactured?
            price = 10000, -- How many gold coins will the blacksmith charge for the service?
            ingredients = {[5880] = 90, [5468] = 2, [2406] = 1}, -- How much of each ingredient will the blacksmith need to work?
            failChance = 10, -- What percentage of chance does the blacksmith have to break the item in the manufacturing process?
            timeDone = 1000 * 60 * 60 * 24 * 2, -- How long does the blacksmith need to finish making the item?
        },
        [2382] = {
            name = "Club",
            price = 15, -- 15 Gold coins
            ingredients = {[3798] = 2}, -- 2x Wooden bars
            failChance = 2, -- 2% of chance to broke this item
            timeDone = 1000 * 60 * 5, -- 5 minutes
        }
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        if msgcontains(msg, tabela[k].name) then
            npcHandler:say("I can create a ".. tabela[k].name .." for you. Do you have these ingredients? ".. tabela[k].ingredients ..".", cid)
            npcHandler.topic[cid] = 1
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "yes") then
            for k, v in pairs(tabela) do
                if player:getId(k) then
                    if player:getMoney(tabela.price) then
                        if player:getItemCount(tabela.ingredients, tabela.ingredients) then
                            if math.random(1, 100) > tabela.failchance then
                                if os.mtime() >= tabela.timeDone then
                                    player:removeMoney(tabela.price)
                                    player:removeItem(tabela.ingredients, tabela.ingredients)
                                    npcHandler:say("Here, your ".. tabela.name ..".", cid)
                                    npcHandler.topic[cid] = 0
                                end
                            else
                                npcHandler:say("Sorry, your item broke.", cid)
                            end
                        else
                            local msg = ""
                            for i = 1, #tabela.ingredients do
                                msg = msg.. ""..ItemType(tabela.ingredients[i]):getName()..""
                                if i < #tabela.ingredient then
                                    msg = msg.. ", "
                                end
                            end
                            npcHandler:say("You don't have ".. msg ..".", cid)
                        end
                    else
                        npcHandler:say("You don't have money.", cid)
                    end
                    break
                end
            end
        elseif msgcontains(msg, "no") then
            npcHandler:say("Okay, come back when you have all the ingredients.", cid)
            npcHandler.topic[cid] = 0
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
lines 34 and 35 don't know what k or v is, since they are not in the loop on line 42. (hence the nil value)
You'll need to create another way for the npc to access the table appropriately.
 
I've modified the script, and the error is now:
Lua Script Error: [Npc interface]
data/npc/scripts/Grutgroc Broadbeard.lua:eek:nCreatureSay
data/npc/scripts/Grutgroc Broadbeard.lua:48: attempt to compare nil with number
stack traceback:
[C]: in function '__lt'
data/npc/scripts/Grutgroc Broadbeard.lua:48: in function 'callback'
data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
data/npc/scripts/Grutgroc Broadbeard.lua:7: in function <data/npc/scripts/Grutgroc Broadbeard.lua:7>
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local tabela = {
        [2392] = { -- What is the item ID that will be manufactured?
            name = "Fire Sword", -- What is the name of the item that will be manufactured?
            price = 10000, -- How many gold coins will the blacksmith charge for the service?
            ingredients = {[5880] = 90, [5468] = 2, [2406] = 1}, -- How much of each ingredient will the blacksmith need to work?
            failChance = 10, -- What percentage of chance does the blacksmith have to break the item in the manufacturing process?
            timeDone = 1000 * 60 * 60 * 24 * 2, -- How long does the blacksmith need to finish making the item?
        },
        [2382] = {
            name = "Club",
            price = 15, -- 15 Gold coins
            ingredients = {[3798] = 2}, -- 2x Wooden bars
            failChance = 2, -- 2% of chance to broke this item
            timeDone = 1000 * 60 * 5, -- 5 minutes
        }
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        for k, v in pairs(tabela) do
            if msgcontains(msg, tabela[k].name) then
                npcHandler:say("I can create a ".. tabela[k].name .." for you. Do you have these ingredients?.", cid)
                npcHandler.topic[cid] = 1
            end
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "yes") then
            for k, v in pairs(tabela) do
                if player:getId(k) then
                    if player:getMoney(tabela.price) then
                        if player:getItemCount(tabela.ingredients, tabela.ingredients) then
                            if math.random(1, 100) > tabela.failchance then
                                if os.mtime() >= tabela.timeDone then
                                    player:removeMoney(tabela.price)
                                    player:removeItem(tabela.ingredients, tabela.ingredients)
                                    npcHandler:say("Here, your ".. tabela.name ..".", cid)
                                    npcHandler.topic[cid] = 0
                                end
                            else
                                npcHandler:say("Sorry, your item broke.", cid)
                            end
                        else
                            local msg = ""
                            for i = 1, #tabela.ingredients do
                                msg = msg.. ""..ItemType(tabela.ingredients[i]):getName()..""
                                if i < #tabela.ingredient then
                                    msg = msg.. ", "
                                end
                            end
                            npcHandler:say("You don't have ".. msg ..".", cid)
                        end
                    else
                        npcHandler:say("You don't have money.", cid)
                    end
                    break
                end
            end
        elseif msgcontains(msg, "no") then
            npcHandler:say("Okay, come back when you have all the ingredients.", cid)
            npcHandler.topic[cid] = 0
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Post automatically merged:

I learned how to use the tables and redid my script, getting much more organized, but I still need to add the ingredients and also the creation time of each item.
Someone help me?
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local itemInfo = {
        ["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            -- ingredients = ??? ??? ???
        },
        ["stonecutter axe"] = {
            itemPrice = 200000,
            itemId = 2431,
            failChance = 5000,
        },
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        for key, v in pairs(itemInfo) do
            if msgcontains(msg, key) then
                npcHandler:say("Posso criar um ".. key ..", voce quer?", cid)
                npcHandler.topic[cid] = 1
            end
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "sim") then
            for key, v in pairs(itemInfo) do
                if player:getStorageValue(FAZENDO_ITEM) <= 0 then
                    if player:getMoney() >= itemInfo[key].itemPrice then
                        if math.random(1, 100000) > itemInfo[key].failChance then
                            player:setStorageValue(FAZENDO_ITEM, 0)
                            player:removeMoney(itemInfo[key].itemPrice)
                            player:addItem(itemInfo[key].itemId, 1, true)                           
                            npcHandler:say("Pronto, pegue seu ".. key ..".", cid)
                            npcHandler.topic[cid] = 0
                        else
                            local newPrice = (itemInfo[key].itemPrice / 2) -- If it fails, the blacksmith will charge half the price for the service.
                            player:removeMoney(newPrice)
                            npcHandler:say("Infelizmente o ".. key .." quebrou. Portanto cobrei apenas ".. newPrice .." moedas de ouro.", cid)
                            npcHandler.topic[cid] = 0
                        end
                    else
                        npcHandler:say("Voce nao tem ".. itemInfo[key].itemPrice .." moedas de ouro.", cid)
                        npcHandler.topic[cid] = 0
                    end
                else
                    npcHandler:say("Ja estou criando um item para voce.", cid)
                    npcHandler.topic[cid] = 0
                end
            end
        elseif msgcontains(msg, "nao") then
            npcHandler:say("Certo, volte sempre!", cid)
            npcHandler.topic[cid] = 0
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

--[[
    List of things to do:

        Add creation time for each item.
            For example:
                A fire sword takes 2 days to be ready.
                A stonecutter axe takes 5 days to be ready.

        Add ingredients for the blacksmith to use in creating the items.
            For example:
                A fire sword needs 10 pieces of steel, a piece of wood, and 2 gold nuggets.
                A stonecutter axe needs 100 pieces of steel.
--]]
Post automatically merged:

Maybe something like this:
ingredients = {
[1] = {name = "fire bug", id = 5468, amount = 2},
[2] = {name = "sabre", id = 2385, amount = 1},
}
Lua:
["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            ingredients = {
                [1] = {name = "fire bug", id = 5468, amount = 2},
                [2] = {name = "sabre", id = 2385, amount = 1},
            }
        },
Help-me xD
 
Last edited:
I've modified the script, and the error is now:

Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local tabela = {
        [2392] = { -- What is the item ID that will be manufactured?
            name = "Fire Sword", -- What is the name of the item that will be manufactured?
            price = 10000, -- How many gold coins will the blacksmith charge for the service?
            ingredients = {[5880] = 90, [5468] = 2, [2406] = 1}, -- How much of each ingredient will the blacksmith need to work?
            failChance = 10, -- What percentage of chance does the blacksmith have to break the item in the manufacturing process?
            timeDone = 1000 * 60 * 60 * 24 * 2, -- How long does the blacksmith need to finish making the item?
        },
        [2382] = {
            name = "Club",
            price = 15, -- 15 Gold coins
            ingredients = {[3798] = 2}, -- 2x Wooden bars
            failChance = 2, -- 2% of chance to broke this item
            timeDone = 1000 * 60 * 5, -- 5 minutes
        }
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        for k, v in pairs(tabela) do
            if msgcontains(msg, tabela[k].name) then
                npcHandler:say("I can create a ".. tabela[k].name .." for you. Do you have these ingredients?.", cid)
                npcHandler.topic[cid] = 1
            end
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "yes") then
            for k, v in pairs(tabela) do
                if player:getId(k) then
                    if player:getMoney(tabela.price) then
                        if player:getItemCount(tabela.ingredients, tabela.ingredients) then
                            if math.random(1, 100) > tabela.failchance then
                                if os.mtime() >= tabela.timeDone then
                                    player:removeMoney(tabela.price)
                                    player:removeItem(tabela.ingredients, tabela.ingredients)
                                    npcHandler:say("Here, your ".. tabela.name ..".", cid)
                                    npcHandler.topic[cid] = 0
                                end
                            else
                                npcHandler:say("Sorry, your item broke.", cid)
                            end
                        else
                            local msg = ""
                            for i = 1, #tabela.ingredients do
                                msg = msg.. ""..ItemType(tabela.ingredients[i]):getName()..""
                                if i < #tabela.ingredient then
                                    msg = msg.. ", "
                                end
                            end
                            npcHandler:say("You don't have ".. msg ..".", cid)
                        end
                    else
                        npcHandler:say("You don't have money.", cid)
                    end
                    break
                end
            end
        elseif msgcontains(msg, "no") then
            npcHandler:say("Okay, come back when you have all the ingredients.", cid)
            npcHandler.topic[cid] = 0
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
Post automatically merged:

I learned how to use the tables and redid my script, getting much more organized, but I still need to add the ingredients and also the creation time of each item.
Someone help me?
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local itemInfo = {
        ["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            -- ingredients = ??? ??? ???
        },
        ["stonecutter axe"] = {
            itemPrice = 200000,
            itemId = 2431,
            failChance = 5000,
        },
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        for key, v in pairs(itemInfo) do
            if msgcontains(msg, key) then
                npcHandler:say("Posso criar um ".. key ..", voce quer?", cid)
                npcHandler.topic[cid] = 1
            end
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "sim") then
            for key, v in pairs(itemInfo) do
                if player:getStorageValue(FAZENDO_ITEM) <= 0 then
                    if player:getMoney() >= itemInfo[key].itemPrice then
                        if math.random(1, 100000) > itemInfo[key].failChance then
                            player:setStorageValue(FAZENDO_ITEM, 0)
                            player:removeMoney(itemInfo[key].itemPrice)
                            player:addItem(itemInfo[key].itemId, 1, true)                         
                            npcHandler:say("Pronto, pegue seu ".. key ..".", cid)
                            npcHandler.topic[cid] = 0
                        else
                            local newPrice = (itemInfo[key].itemPrice / 2) -- If it fails, the blacksmith will charge half the price for the service.
                            player:removeMoney(newPrice)
                            npcHandler:say("Infelizmente o ".. key .." quebrou. Portanto cobrei apenas ".. newPrice .." moedas de ouro.", cid)
                            npcHandler.topic[cid] = 0
                        end
                    else
                        npcHandler:say("Voce nao tem ".. itemInfo[key].itemPrice .." moedas de ouro.", cid)
                        npcHandler.topic[cid] = 0
                    end
                else
                    npcHandler:say("Ja estou criando um item para voce.", cid)
                    npcHandler.topic[cid] = 0
                end
            end
        elseif msgcontains(msg, "nao") then
            npcHandler:say("Certo, volte sempre!", cid)
            npcHandler.topic[cid] = 0
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

--[[
    List of things to do:

        Add creation time for each item.
            For example:
                A fire sword takes 2 days to be ready.
                A stonecutter axe takes 5 days to be ready.

        Add ingredients for the blacksmith to use in creating the items.
            For example:
                A fire sword needs 10 pieces of steel, a piece of wood, and 2 gold nuggets.
                A stonecutter axe needs 100 pieces of steel.
--]]
Post automatically merged:

Maybe something like this:

Lua:
["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            ingredients = {
                [1] = {name = "fire bug", id = 5468, amount = 2},
                [2] = {name = "sabre", id = 2385, amount = 1},
            }
        },
Help-me xD
Okay, you're at least trying. xD

So basically you're getting stuck because you're using a loop when you no longer need to be using a loop.
What you're going to want to do is hold the information you collect from the first loop to use later.

this line will be where we store the information. (place at top of script)
Lua:
local temp_key = {}
this line is going to be our table index. (already in your script)
Lua:
local player = Player(cid)
In our first loop, we will store the table index we find in the loop
Lua:
if npcHandler.topic[cid] <= 0 then
    for key, v in pairs(itemInfo) do
        if msgcontains(msg, key) then
            npcHandler:say("Posso criar um ".. key ..", voce quer?", cid)
            npcHandler.topic[cid] = 1
            temp_key[player] = msg -- storing the information
            break -- once you've found the information you're looking for, we break out of the loop to save time/resources
        end
    end
end
sidenote, you shouldn't even need to use a loop here, as you could do something like this..
(copy paste the below code into Lua test demo, to see it in action. Lua: demo (https://www.lua.org/cgi-bin/demo))
Lua:
local blah = {
    ["firesword"] = 7,
    ["gold"] = 5
}

local msg = "firesword"
if blah[msg] then
    print(blah[msg])
end
But, continuing on..
Now that you know the index, you can get rid of the loop down below.
Protip: if you know that no matter what happens after the player says yes/no you're going to set their talkstate back to 0, you don't need to put it 50 times in the script. xP You can just put it on the last line in the script.
Also as a best practice in npc's, you generally only want 1 if statement for the entire thing since the npc is typically only ever going to do 1 action, so learn to use elseif's liberally.
Lua:
if npcHandler.topic[cid] == 1 then
    if msgcontains(msg, "sim") then
        --for key, v in pairs(itemInfo) do -- removing the loop
            if player:getStorageValue(FAZENDO_ITEM) <= 0 then
                if player:getMoney() >= itemInfo[temp_key[player]].itemPrice then -- replacing every 'key' from the loop for 'temp_key[player]' which is where we are storing the information
                    if math.random(1, 100000) > itemInfo[temp_key[player]].failChance then -- replaced key here
                        player:setStorageValue(FAZENDO_ITEM, 0)
                        player:removeMoney(itemInfo[temp_key[player]].itemPrice) -- replaced key here
                        player:addItem(itemInfo[temp_key[player]].itemId, 1, true)   -- replaced key here                        
                        npcHandler:say("Pronto, pegue seu ".. temp_key[player] ..".", cid) -- replaced key here
                        -- npcHandler.topic[cid] = 0 -- only required once, at end.
                    else
                        local newPrice = (itemInfo[temp_key[player]].itemPrice / 2) -- If it fails, the blacksmith will charge half the price for the service. ------- -- replaced key here
                        player:removeMoney(newPrice)
                        npcHandler:say("Infelizmente o ".. temp_key[player] .." quebrou. Portanto cobrei apenas ".. newPrice .." moedas de ouro.", cid) -- replaced key here
                        -- npcHandler.topic[cid] = 0 -- only required once, at end.
                    end
                else
                    npcHandler:say("Voce nao tem ".. itemInfo[temp_key[player]].itemPrice .." moedas de ouro.", cid) -- replaced key here
                    -- npcHandler.topic[cid] = 0 -- only required once, at end.
                end
            else
                npcHandler:say("Ja estou criando um item para voce.", cid)
                -- npcHandler.topic[cid] = 0 -- only required once, at end.
            end
        --end -- removing the loop
    elseif msgcontains(msg, "nao") then
        npcHandler:say("Certo, volte sempre!", cid)
        -- npcHandler.topic[cid] = 0 -- only required once, at end.
    end
    npcHandler.topic[cid] = 0 -- here!
end
And that should get us where we want to be.

Let us know how it goes!
 
Now, the NPC asks if I want a fire sword.
15:44 Grutgroc Broadbeard: Posso criar um fire sword, voce quer?

Until this part of the script, everything is OK. It was good, however, the NPC does not react when answering his question.
15:44 Cerusacan [2]: sim

Currently the script looks like this:
Lua:
local temp_key = {}
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local itemInfo = {
        ["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            -- ingredients = ??? ??? ???
        },
        ["stonecutter axe"] = {
            itemPrice = 200000,
            itemId = 2431,
            failChance = 5000,
        },
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        for key, v in pairs(itemInfo) do
            if msgcontains(msg, key) then
                npcHandler:say("Posso criar um ".. key ..", voce quer?", cid)
                npcHandler.topic[cid] = 1
                temp_key[player] = msg -- storing the information
                break -- once you've found the information you're looking for, we break out of the loop to save time/resources
            end
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "sim") then
            if player:getStorageValue(FAZENDO_ITEM) <= 0 then
                if player:getMoney() >= itemInfo[temp_key[player]].itemPrice then -- replacing every 'key' from the loop for 'temp_key[player]' which is where we are storing the information
                    if math.random(1, 100000) > itemInfo[temp_key[player]].failChance then -- replaced key here
                        player:setStorageValue(FAZENDO_ITEM, 0)
                        player:removeMoney(itemInfo[temp_key[player]].itemPrice) -- replaced key here
                        player:addItem(itemInfo[temp_key[player]].itemId, 1, true)   -- replaced key here                       
                        npcHandler:say("Pronto, pegue seu ".. temp_key[player] ..".", cid) -- replaced key here
                    else
                        local newPrice = (itemInfo[temp_key[player]].itemPrice / 2) -- If it fails, the blacksmith will charge half the price for the service. ------- -- replaced key here
                        player:removeMoney(newPrice)
                        npcHandler:say("Infelizmente o ".. temp_key[player] .." quebrou. Portanto cobrei apenas ".. newPrice .." moedas de ouro.", cid) -- replaced key here
                    end
                else
                    npcHandler:say("Voce nao tem ".. itemInfo[temp_key[player]].itemPrice .." moedas de ouro.", cid) -- replaced key here
                end
            else
                npcHandler:say("Ja estou criando um item para voce.", cid)
            end
        elseif msgcontains(msg, "nao") then
            npcHandler:say("Certo, volte sempre!", cid)
        end
        npcHandler.topic[cid] = 0
    end
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

--[[
    List of things to do:

        Add creation time for each item.
            For example:
                A fire sword takes 2 days to be ready.
                A stonecutter axe takes 5 days to be ready.

        Add ingredients for the blacksmith to use in creating the items.
            For example:
                A fire sword needs 10 pieces of steel, a piece of wood, and 2 gold nuggets.
                A stonecutter axe needs 100 pieces of steel.
--]]
 
Now, the NPC asks if I want a fire sword.


Until this part of the script, everything is OK. It was good, however, the NPC does not react when answering his question.


Currently the script looks like this:
Lua:
local temp_key = {}
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local itemInfo = {
        ["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            -- ingredients = ??? ??? ???
        },
        ["stonecutter axe"] = {
            itemPrice = 200000,
            itemId = 2431,
            failChance = 5000,
        },
    }

    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)

    if npcHandler.topic[cid] <= 0 then
        for key, v in pairs(itemInfo) do
            if msgcontains(msg, key) then
                npcHandler:say("Posso criar um ".. key ..", voce quer?", cid)
                npcHandler.topic[cid] = 1
                temp_key[player] = msg -- storing the information
                break -- once you've found the information you're looking for, we break out of the loop to save time/resources
            end
        end
    end

    if npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "sim") then
            if player:getStorageValue(FAZENDO_ITEM) <= 0 then
                if player:getMoney() >= itemInfo[temp_key[player]].itemPrice then -- replacing every 'key' from the loop for 'temp_key[player]' which is where we are storing the information
                    if math.random(1, 100000) > itemInfo[temp_key[player]].failChance then -- replaced key here
                        player:setStorageValue(FAZENDO_ITEM, 0)
                        player:removeMoney(itemInfo[temp_key[player]].itemPrice) -- replaced key here
                        player:addItem(itemInfo[temp_key[player]].itemId, 1, true)   -- replaced key here                      
                        npcHandler:say("Pronto, pegue seu ".. temp_key[player] ..".", cid) -- replaced key here
                    else
                        local newPrice = (itemInfo[temp_key[player]].itemPrice / 2) -- If it fails, the blacksmith will charge half the price for the service. ------- -- replaced key here
                        player:removeMoney(newPrice)
                        npcHandler:say("Infelizmente o ".. temp_key[player] .." quebrou. Portanto cobrei apenas ".. newPrice .." moedas de ouro.", cid) -- replaced key here
                    end
                else
                    npcHandler:say("Voce nao tem ".. itemInfo[temp_key[player]].itemPrice .." moedas de ouro.", cid) -- replaced key here
                end
            else
                npcHandler:say("Ja estou criando um item para voce.", cid)
            end
        elseif msgcontains(msg, "nao") then
            npcHandler:say("Certo, volte sempre!", cid)
        end
        npcHandler.topic[cid] = 0
    end
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

--[[
    List of things to do:

        Add creation time for each item.
            For example:
                A fire sword takes 2 days to be ready.
                A stonecutter axe takes 5 days to be ready.

        Add ingredients for the blacksmith to use in creating the items.
            For example:
                A fire sword needs 10 pieces of steel, a piece of wood, and 2 gold nuggets.
                A stonecutter axe needs 100 pieces of steel.
--]]
When you said 'sim' did you check the console for errors?

If no errors start putting print(1), print(2) et cetera throughout the script to find out where it's breaking.
 
Lua:
local temp_key = {}
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

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

local function creatureSayCallback(cid, type, msg)
    local itemInfo = {
        ["fire sword"] = {            -- What is the name of the item to be manufactured?
            itemPrice = 4200,        -- How much gold will the blacksmith ask for to create the item?
            itemId = 2392,            -- What is the item ID?
            failChance = 3500,        -- What percentage of item creation failed?
            -- timeDone = 1000 * 60 * 60 * 24 * 2
            -- ingredients = ??? ??? ???
        },
        ["stonecutter axe"] = {
            itemPrice = 200000,
            itemId = 2431,
            failChance = 5000,
        },
    }
    
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    
    if npcHandler.topic[cid] <= 0 then
        if itemInfo[msg] then
            npcHandler:say("Posso criar um ".. msg ..", voce quer?", cid)
            npcHandler.topic[cid] = 1
            temp_key[cid] = msg
        end

    
    elseif npcHandler.topic[cid] == 1 then
        if msgcontains(msg, "sim") then
            if player:getStorageValue(FAZENDO_ITEM) <= 0 then
                if player:getMoney() >= itemInfo[temp_key[cid]].itemPrice then
                    if math.random(1, 100000) > itemInfo[temp_key[cid]].failChance then
                        player:setStorageValue(FAZENDO_ITEM, 0)
                        player:removeMoney(itemInfo[temp_key[cid]].itemPrice)
                        player:addItem(itemInfo[temp_key[cid]].itemId, 1, true)                      
                        npcHandler:say("Pronto, pegue seu ".. temp_key[cid] ..".", cid)
                    else
                        local newPrice = (itemInfo[temp_key[cid]].itemPrice / 2) -- If it fails, the blacksmith will charge half the price for the service.
                        player:removeMoney(newPrice)
                        npcHandler:say("Infelizmente o ".. temp_key[cid] .." quebrou. Portanto cobrei apenas ".. newPrice .." moedas de ouro.", cid)
                    end
                else
                    npcHandler:say("Voce nao tem ".. itemInfo[temp_key[cid]].itemPrice .." moedas de ouro.", cid)
                end
            else
                npcHandler:say("Ja estou criando um item para voce.", cid)
            end
            npcHandler.topic[cid] = 0
        elseif msgcontains(msg, "nao") then
            npcHandler:say("Certo, volte sempre!", cid)
            npcHandler.topic[cid] = 0
        else
            npcHandler:say("'sim' or 'nao'.. You've said: '" .. msg .. "'", cid)
        end
    end
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

--[[
    List of things to do:

        Add creation time for each item.
            For example:
                A fire sword takes 2 days to be ready.
                A stonecutter axe takes 5 days to be ready.

        Add ingredients for the blacksmith to use in creating the items.
            For example:
                A fire sword needs 10 pieces of steel, a piece of wood, and 2 gold nuggets.
                A stonecutter axe needs 100 pieces of steel.
--]]
 
Solution
Back
Top