• 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 NPC give first weapon for knight (my first script)

kibo433

New Member
Joined
Sep 6, 2010
Messages
12
Solutions
1
Reaction score
3
Hi! I wrote my first script. It based on script for first wand/rod. (I am using TFS 1.5 downgrade to 8.6).
So... I wanna make a npc, which will give first sword/axe/club for a knight/elite knight.
This is my code:
Lua:
local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    if msgcontains(msg, "first weapon") and player:getVocation():getBase():getId() == 4 then
        npcHandler:say("What kind of weapon do you prefer? A {sword}, an {axe} or a {club}?", cid)
        if msgcontains(msg, "sword") and player:getVocation():getBase():getId() == 4 then
            if player:getStorageValue(PlayerStorageKeys.firstWeapon) == -1 then
                npcHandler:say("So you ask me for a jagged sword to begin your adventure?", cid)
                npcHandler.topic[cid] = 1
            else
                npcHandler:say("What? I have already gave you one jagged sword!", cid)
            end
        elseif msgcontains(msg, "axe") and player:getVocation():getBase():getId() == 4 then
            if player:getStorageValue(PlayerStorageKeys.firstWeapon) == -1 then
                npcHandler:say("So you ask me for a steel axe to begin your adventure?", cid)
                npcHandler.topic[cid] = 2
            else
                npcHandler:say("What? I have already gave you one steel axe!", cid)
            end
        elseif msgcontains(msg, "club") and player:getVocation():getBase():getId() == 4 then
            if player:getStorageValue(PlayerStorageKeys.firstWeapon) == -1 then
                npcHandler:say("So you ask me for a daramanian mace to begin your adventure?", cid)
                npcHandler.topic[cid] = 3
            else
                npcHandler:say("What? I have already gave you one daramian mace!", cid)
            end
        end
    elseif msgcontains(msg, "yes") then
        if npcHandler.topic[cid] == 1 then
            player:addItem(8602, 1)
            npcHandler:say("Here you are young adept, take care yourself.", cid)
            player:setStorageValue(PlayerStorageKeys.firstWeapon, 1)
        elseif npcHandler.topic[cid] == 2 then
            player:addItem(8601, 1)
            npcHandler:say("Here you are young adept, take care yourself.", cid)
            player:setStorageValue(PlayerStorageKeys.firstWeapon, 1)
        elseif npcHandler.topic[cid] == 3 then
            player:addItem(2439, 1)
            npcHandler:say("Here you are young adept, take care yourself.", cid)
            player:setStorageValue(PlayerStorageKeys.firstWeapon, 1)
        end
            npcHandler.topic[cid] = 0
    elseif msgcontains(msg, "no") and npcHandler.topic[cid] == 1 then
            npcHandler:say("Ok, then.", cid)
            npcHandler.topic[cid] = 0
    elseif msgcontains(msg, "first weapon") and player:getVocation():getBase():getId() ~= 4 then
        npcHandler:say("Sorry, you aren\'t a knight.",cid)
    end
    return true
end

The script works until the frase: "What kind of weapon do you prefer...?". When I am asking about sword/club/axe, there is no reaction from NPC and console (zero errors).
What I missed?
And I have a question about assigning storage value. I must register that somewhere before? And why there is value -1 and that works (in script with wand/rod)?
Sorry for my question and knowledge. I am learning yet.
 
Solution
I took it and made some simple changes, and it's easier to understand now. Give it a try. If it works, mark it as the preferred answer (solution) later on.

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 function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player =...
I took it and made some simple changes, and it's easier to understand now. Give it a try. If it works, mark it as the preferred answer (solution) later on.

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 function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local player = Player(cid)

    if msgcontains(msg, "first weapon") then
        if player:getStorageValue(PlayerStorageKeys.firstWeapon) == 0 then
            if player:getVocation():getId() == 4 then
                npcHandler:say("What kind of weapon do you prefer? A {sword}, an {axe}, or a {club}?", cid)
                npcHandler.topic[cid] = 1
                npcHandler.itemChoice = {}
            else
                npcHandler:say("Sorry, you aren\'t a knight.", cid)
            end
        else
            npcHandler:say("What? I have already given you a weapon!", cid)
        end
    elseif npcHandler.topic[cid] == 1 then
        local itemName
        local itemID

        if msgcontains(msg, "sword") then
            itemName = "jagged sword"
            itemID = 8602
        elseif msgcontains(msg, "axe") then
            itemName = "steel axe"
            itemID = 8601
        elseif msgcontains(msg, "club") then
            itemName = "daramanian mace"
            itemID = 2439
        else
            npcHandler:say("I didn't understand, sorry.", cid)
            return true
        end

        npcHandler:say("So you ask me for a {" .. itemName .. "} to begin your adventure?", cid)
        npcHandler.topic[cid] = 2
        npcHandler.itemChoice[cid] = itemID
    elseif npcHandler.topic[cid] == 2 then
        if msgcontains(msg, "yes") then
            local itemID = npcHandler.itemChoice[cid]

            if player:getFreeCapacity() >= ItemType(itemID):getWeight(1) then
                player:addItem(itemID, 1)
                npcHandler:say("Here you are young adept. Take care of yourself.", cid)
                player:setStorageValue(PlayerStorageKeys.firstWeapon, 1)
            else
                npcHandler:say("You don't have enough capacity to carry this item.", cid)
            end
        elseif msgcontains(msg, "no") then
            npcHandler:say("Ok, then.", cid)
        else
            npcHandler:say("I didn't understand, sorry.", cid)
        end

        npcHandler.topic[cid] = 0
        npcHandler.itemChoice[cid] = nil
    end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Solution
It's working. Thank you Mateus! I had to make some fixes. First is change getStorageValue for -1. Now I know that is default value. Second is to add "getBase()" for player:getVocation():getId(). Now it's working perfect.
 
Back
Top