• 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+ Npc problem with conversion 0.4 to 1.2

V i t t u

New Member
Joined
May 25, 2012
Messages
12
Reaction score
0
Location
Brazil
error in npc
i tried convert script 0.4 for tfs 1.2

Code:
Lua Script Error: [Npc interface]
data/npc/scripts/novicespells.lua:onCreatureSay
data/npc/scripts/novicespells.lua:44: attempt to index global 'talkState' (a nil value)
stack traceback:
        [C]: in function '__index'
        data/npc/scripts/novicespells.lua:44: in function 'callback'
        data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
        data/npc/scripts/novicespells.lua:7: in function <data/npc/scripts/novicespells.lua:7>

Npc.lua
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
npcHandler:addModule(FocusModule:new())

local storage = 98534
local spells = {"Light Healing", "Recovery"}

local text = ""
for x = 1, #spells do
     local c = ", "
     if x == 1 then
         c = ""
     elseif x == #spells then
         c = " and "
     end
     text = text .. c
     text = text .. spells[x]
end

function creatureSayCallback(cid, type, msg)

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

     if not npcHandler:isFocused(cid) then
         if msg == "hi" or msg == "hello" then
             if player:getStorageValue(config.storage) == -1 then
                 npcHandler:say("Hello dear "..getCreatureName(cid)..", I can teach you some interesting {spells}, but remember you can't learn it all.", cid)
                 talkState[talkUser] = 1
             else
                 npcHandler:say("Hello, hope you're doing well with your new spells.", cid)
             end
             npcHandler:addFocus(cid)
         else
             return false
         end
     end

     if msgcontains(msg, "spells") and talkState[talkUser] == 1 then
         npcHandler:say("I can teach you spells: "..text..". But remember, you can choose only 2 spells to learn from my list, other spells will be pernament not available to you.", cid)
         npcHandler:say("So which spells do you choose?", cid)
         talkState[talkUser] = 2
     elseif msgcontains(msg, "bye") then
         npcHandler:say("Bye.", cid)
         npcHandler:releaseFocus(cid)
     elseif talkState[talkUser] == 2 then
         if isInArray(spells, msg) then
             if player:getStorageValue(config.storage) < 1 then
                 npcHandler:say(player:getStorageValue(config.storage) == -1 and "You have now learned the spell "..msg..", you can choose 1 more spell."  or "So "..msg.." will be your second spell, good luck with both spells.", cid)
                 player:learnInstantSpell(msg)
                 player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE)
                 player:setStorageValue(config.storage, player:getStorageValue(config.storage) + 1)
                 npcHandler.topic[cid] = 0
             else
                 npcHandler:say("Sorry, you already learned 2 spells from me.", cid)
                 npcHandler.topic[cid] = 0
             end
         else
             npcHandler:say(msg.." is not a spell you can learn from me, you can choose "..text..".", cid)
         end
     end
     return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Solution
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

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 storage = 98534
local spells = {"{Light Healing}", "{Intense Healing}", "{Ultimate Healing}", "{Divine Healing}", "{Wound Cleansing}"}

local text = ""
for x = 1, #spells do
     local c = ", "
     if x == 1 then...
The error message "attempt to index global 'variable name' (a nil value)
Means that you are trying to use a variable that doesn't exist because it has yet to be initialized.
In this case you have to initialize 'talkState', to do so add
Lua:
local talkState = {}
before function creatureSayCallback(cid, type, msg)
 
no errors happen but the script doesn't work
i need to convert this npc to tfs 1.2

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

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 storage = 98534
local spells = {"Light Healing", "Intense Healing", "Ultimate Healing", "Divine Healing", "Wound Cleansing"}

local text = ""
for x = 1, #spells do
     local c = ", "
     if x == 1 then
         c = ""
     elseif x == #spells then
         c = " and "
     end
     text = text .. c
     text = text .. spells[x]
end

function creatureSayCallback(cid, type, msg)

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

     if not npcHandler:isFocused(cid) then
         if msg == "hi" or msg == "hello" then
             if getPlayerStorageValue(cid, storage) == -1 then
                 selfSay("Hello dear "..getCreatureName(cid)..", I can teach you some interesting {spells}, but remember you can't learn it all.", cid)
                 talkState[talkUser] = 1
             else
                 selfSay("Hello, hope you're doing well with your new spells.", cid)
             end
             npcHandler:addFocus(cid)
         else
             return false
         end
     end

     if msgcontains(msg, "spells") and talkState[talkUser] == 1 then
         selfSay("I can teach you spells: "..text..". But remember, you can choose only 2 spells to learn from my list, other spells will be pernament not available to you.", cid)
         selfSay("So which spells do you choose?", cid)
         talkState[talkUser] = 2
     elseif msgcontains(msg, "bye") then
         selfSay("Bye.", cid)
         npcHandler:releaseFocus(cid)
     elseif talkState[talkUser] == 2 then
         if isInArray(spells, msg) then
             if getPlayerStorageValue(cid, storage) < 1 then
                 selfSay(getPlayerStorageValue(cid, storage) == -1 and "You have now learned the spell "..msg..", you can choose 1 more spell."  or "So "..msg.." will be your second spell, good luck with both spells.", cid)
                 doPlayerLearnInstantSpell(cid, msg)
                 setPlayerStorageValue(cid, storage, getPlayerStorageValue(cid, storage) + 1)
             else
                 selfSay("Sorry, you already learned 2 spells from me.", cid)
                 talkState[talkUser] = 0
             end
         else
             selfSay(msg.." is not a spell you can learn from me, you can choose "..text..".", cid)
         end
     end
     return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Lua:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState = {}

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 storage = 98534
local spells = {"{Light Healing}", "{Intense Healing}", "{Ultimate Healing}", "{Divine Healing}", "{Wound Cleansing}"}

local text = ""
for x = 1, #spells do
     local c = ", "
     if x == 1 then
         c = ""
     elseif x == #spells then
         c = " and "
     end
     text = text .. c
     text = text .. spells[x]
end

function creatureSayCallback(cid, type, msg)

    local player = Player(cid)
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    if not npcHandler:isFocused(cid) then
        if msg == "hi" or msg == "hello" then
            if player:getStorageValue(storage) == -1 then
                selfSay("Hello dear "..player:getName()..", I can teach you some interesting {spells}, but remember you can't learn it all.", cid)
                talkState[talkUser] = 1
            else
                selfSay("Hello, hope you're doing well with your new spells.", cid)
            end
            npcHandler:addFocus(cid)
        else
            return false
        end
    end

    if msgcontains(msg, "spells") and talkState[talkUser] == 1 then
        selfSay("I can teach you spells: "..text..". But remember, you can choose only 2 spells to learn from my list, other spells will be pernament not available to you.", cid)
        selfSay("So which spells do you choose?", cid)
        talkState[talkUser] = 2
    elseif msgcontains(msg, "bye") then
        selfSay("Bye.", cid)
        npcHandler:releaseFocus(cid)
    elseif talkState[talkUser] == 2 then
        local m = "{"..msg.."}"
        if isInArray(spells, m) then
            if player:getStorageValue(storage) < 1 then
                if not player:hasLearnedSpell(msg) then
                    player:learnSpell(msg)
                    selfSay(player:getStorageValue(storage) == -1 and "You have now learned the spell "..msg..", you can choose 1 more spell."  or "So "..msg.." will be your second spell, good luck with both spells.", cid)
                    player:setStorageValue(storage, player:getStorageValue(storage) + 1)
                else
                    selfSay("You already know that spell.", cid)
                end
            else
                selfSay("Sorry, you already learned 2 spells from me.", cid)
                talkState[talkUser] = 0
            end
        else
            selfSay(m.." is not a spell you can learn from me, you can choose "..text..".", cid)
        end
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
 
Solution
Back
Top