• 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 problem.

samco

4x4 Developer.
Joined
Jul 3, 2007
Messages
1,077
Solutions
9
Reaction score
260
Location
Spain
Hello Otland,

I'm trying to modify my current Quentin NPC so he is capable to sell all blessings. I suppossed that i could add the piece of code that Henricus has and it would work. I was wrong.

This is the part i added:
Lua:
local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    local totalBlessPrice = getBlessingsCost(player:getLevel()) * 5 * 1.3

    if msgcontains(msg, "myass") then
        npcHandler:say("Do you want to receive all five available blessings - for " .. totalBlessPrice .. " gold?", cid)
        npcHandler.topic[cid] = 7
    elseif msgcontains(msg, "yes") then
        if npcHandler.topic[cid] == 7 then
            if player:getBlessings() == 5 then
                npcHandler:say("You already have been blessed!", cid)
            elseif player:removeMoneyNpc(totalBlessPrice) then
                npcHandler:say("You have been blessed by all of five gods!, |PLAYERNAME|.", cid)
                for b = 2, 6 do
                    player:addBlessing(b,1)
                end
                player:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
            else
                npcHandler:say("Come back when you have enough money.", cid)
            end
        end
    end
    return true
end

This is complete script of Quentin 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)
    if not npcHandler:isFocused(cid) then
        return false
    end
    local player = Player(cid)
    local totalBlessPrice = getBlessingsCost(player:getLevel()) * 5 * 1.3

    if msgcontains(msg, "myass") then
        npcHandler:say("Do you want to receive all five available blessings - for " .. totalBlessPrice .. " gold?", cid)
        npcHandler.topic[cid] = 7
    elseif msgcontains(msg, "yes") then
        if npcHandler.topic[cid] == 7 then
            if player:getBlessings() == 5 then
                npcHandler:say("You already have been blessed!", cid)
            elseif player:removeMoneyNpc(totalBlessPrice) then
                npcHandler:say("You have been blessed by all of five gods!, |PLAYERNAME|.", cid)
                for b = 2, 6 do
                    player:addBlessing(b,1)
                end
                player:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
            else
                npcHandler:say("Come back when you have enough money.", cid)
            end
        end
    end
    return true
end


-- Wooden Stake Quest
local stakeKeyword = keywordHandler:addKeyword({'stake'}, StdModule.say, {npcHandler = npcHandler,
        text = {
            'A blessed stake to defeat evil spirits? I do know an old prayer which is said to grant sacred power and to be able to bind this power to someone, or something. ...',
            'However, this prayer needs the combined energy of ten priests. Each of them has to say one line of the prayer. ...',
            'I could start with the prayer, but since the next priest has to be in a different location, you probably will have to travel a lot. ...',
            'Is this stake really important enough to you so that you are willing to take this burden?',
        }}, function(player) return player:getStorageValue(Storage.FriendsandTraders.TheBlessedStake) == -1 end
    )
    stakeKeyword:addChildKeyword({'yes'}, StdModule.say, {npcHandler = npcHandler, text = 'Alright, I guess you need a stake first. Maybe Gamon can help you, the leg of a chair or something could just do. Try asking him for a stake, and if you have one, bring it back to me.', reset = true, ungreet = true}, nil, function(player) player:setStorageValue(Storage.FriendsandTraders.DefaultStart, 1) player:setStorageValue(Storage.FriendsandTraders.TheBlessedStake, 1) end)

-- First prayer
keywordHandler:addKeyword({'stake'}, StdModule.say, {npcHandler = npcHandler, text = 'I guess you couldn\'t convince Gamon to give you a stake, eh?'}, function(player) return player:getStorageValue(Storage.FriendsandTraders.TheBlessedStake) == 1 and player:getItemCount(5941) == 0 end)

local stakeKeyword = keywordHandler:addKeyword({'stake'}, StdModule.say, {npcHandler = npcHandler, text = 'Yes, I was informed what to do. Are you prepared to receive my line of the prayer?'}, function(player) return player:getStorageValue(Storage.FriendsandTraders.TheBlessedStake) == 1 end)
    stakeKeyword:addChildKeyword({'yes'}, StdModule.say, {npcHandler = npcHandler, text = 'So receive my prayer: \'Light shall be near - and darkness afar\'. Now, bring your stake to Tibra in the Carlin church for the next line of the prayer. I will inform her what to do.', reset = true}, nil,
        function(player) player:setStorageValue(Storage.FriendsandTraders.TheBlessedStake, 2) player:getPosition():sendMagicEffect(CONST_ME_MAGIC_BLUE) end
    )
    stakeKeyword:addChildKeyword({''}, StdModule.say, {npcHandler = npcHandler, text = 'I will wait for you.', reset = true})

keywordHandler:addKeyword({'stake'}, StdModule.say, {npcHandler = npcHandler, text = 'You should visit Tibra in the Carlin church now.'}, function(player) return player:getStorageValue(Storage.FriendsandTraders.TheBlessedStake) == 2 end)
keywordHandler:addKeyword({'stake'}, StdModule.say, {npcHandler = npcHandler, text = 'You already received my line of the prayer.'})

-- Twist of Fate
local blessKeyword = keywordHandler:addKeyword({'twist of fate'}, StdModule.say, {npcHandler = npcHandler,
    text = {
        'This is a special blessing I can bestow upon you once you have obtained at least one of the other blessings and which functions a bit differently. ...',
        'It only works when you\'re killed by other adventurers, which means that at least half of the damage leading to your death was caused by others, not by monsters or the environment. ...',
        'The {twist of fate} will not reduce the death penalty like the other blessings, but instead prevent you from losing your other blessings as well as the amulet of loss, should you wear one. It costs the same as the other blessings. ...',
        'Would you like to receive that protection for a sacrifice of |PVPBLESSCOST| gold, child?'
    }})
    blessKeyword:addChildKeyword({'yes'}, StdModule.bless, {npcHandler = npcHandler, text = 'So receive the protection of the twist of fate, pilgrim.', cost = '|PVPBLESSCOST|', bless = 1})
    blessKeyword:addChildKeyword({''}, StdModule.say, {npcHandler = npcHandler, text = 'Fine. You are free to decline my offer.', reset = true})

-- Adventurer Stone
keywordHandler:addKeyword({'adventurer stone'}, StdModule.say, {npcHandler = npcHandler, text = 'Keep your adventurer\'s stone well.'}, function(player) return player:getItemById(18559, true) end)

local stoneKeyword = keywordHandler:addKeyword({'adventurer stone'}, StdModule.say, {npcHandler = npcHandler, text = 'Ah, you want to replace your adventurer\'s stone for free?'}, function(player) return player:getStorageValue(Storage.AdventurersGuild.FreeStone.Quentin) ~= 1 end)
    stoneKeyword:addChildKeyword({'yes'}, StdModule.say, {npcHandler = npcHandler, text = 'Here you are. Take care.', reset = true}, nil, function(player) player:addItem(18559, 1) player:setStorageValue(Storage.AdventurersGuild.FreeStone.Quentin, 1) end)
    stoneKeyword:addChildKeyword({''}, StdModule.say, {npcHandler = npcHandler, text = 'No problem.', reset = true})

local stoneKeyword = keywordHandler:addKeyword({'adventurer stone'}, StdModule.say, {npcHandler = npcHandler, text = 'Ah, you want to replace your adventurer\'s stone for 30 gold?'})
    stoneKeyword:addChildKeyword({'yes'}, StdModule.say, {npcHandler = npcHandler, text = 'Here you are. Take care.', reset = true},
        function(player) return player:getMoney() + player:getBankBalance() >= 30 end,
        function(player) if player:removeMoneyNpc(30) then player:addItem(18559, 1) end end
    )
    stoneKeyword:addChildKeyword({'yes'}, StdModule.say, {npcHandler = npcHandler, text = 'Sorry, you don\'t have enough money.', reset = true})
    stoneKeyword:addChildKeyword({''}, StdModule.say, {npcHandler = npcHandler, text = 'No problem.', reset = true})

-- Healing
local function addHealKeyword(text, condition, effect)
    keywordHandler:addKeyword({'heal'}, StdModule.say, {npcHandler = npcHandler, text = text},
        function(player) return player:getCondition(condition) ~= nil end,
        function(player)
            player:removeCondition(condition)
            player:getPosition():sendMagicEffect(effect)
        end
    )
end

addHealKeyword('You are burning. Let me quench those flames.', CONDITION_FIRE, CONST_ME_MAGIC_GREEN)
addHealKeyword('You are poisoned. Let me soothe your pain.', CONDITION_POISON, CONST_ME_MAGIC_RED)
addHealKeyword('You are electrified, my child. Let me help you to stop trembling.', CONDITION_ENERGY, CONST_ME_MAGIC_GREEN)

keywordHandler:addKeyword({'heal'}, StdModule.say, {npcHandler = npcHandler, text = 'You are hurt, my child. I will heal your wounds.'},
    function(player) return player:getHealth() < 40 end,
    function(player)
        local health = player:getHealth()
        if health < 40 then player:addHealth(40 - health) end
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_GREEN)
    end
)
keywordHandler:addKeyword({'heal'}, StdModule.say, {npcHandler = npcHandler, text = 'You aren\'t looking that bad. Sorry, I can\'t help you. But if you are looking for additional protection you should go on the {pilgrimage} of ashes or get the protection of the {twist of fate} here.'})

-- Basic
keywordHandler:addKeyword({'pilgrimage'}, StdModule.say, {npcHandler = npcHandler, text = 'Whenever you receive a lethal wound, your vital force is damaged and there is a chance that you lose some of your equipment. With every single of the five {blessings} you have, this damage and chance of loss will be reduced.'})
keywordHandler:addKeyword({'spiritual'}, StdModule.say, {npcHandler = npcHandler, text = 'I see you received the spiritual shielding in the whiteflower temple south of Thais.'}, function(player) return player:hasBlessing(1) end)
keywordHandler:addAliasKeyword({'shield'})
keywordHandler:addKeyword({'embrace'}, StdModule.say, {npcHandler = npcHandler, text = 'I can sense that the druids north of Carlin have provided you with the Embrace of Tibia.'}, function(player) return player:hasBlessing(2) end)
keywordHandler:addKeyword({'suns'}, StdModule.say, {npcHandler = npcHandler, text = 'I can see you received the blessing of the two suns in the suntower near Ab\'Dendriel.'}, function(player) return player:hasBlessing(3) end)
keywordHandler:addAliasKeyword({'fire'})
keywordHandler:addKeyword({'phoenix'}, StdModule.say, {npcHandler = npcHandler, text = 'I can sense that the spark of the phoenix already was given to you by the dwarven priests of earth and fire in Kazordoon.'}, function(player) return player:hasBlessing(4) end)
keywordHandler:addAliasKeyword({'spark'})
keywordHandler:addKeyword({'solitude'}, StdModule.say, {npcHandler = npcHandler, text = 'I can sense you already talked to the hermit Eremo on the isle of Cormaya and received this blessing.'}, function(player) return player:hasBlessing(5) end)
keywordHandler:addAliasKeyword({'wisdom'})
keywordHandler:addKeyword({'spiritual'}, StdModule.say, {npcHandler = npcHandler, text = 'You can ask for the blessing of spiritual shielding in the whiteflower temple south of Thais.'})
keywordHandler:addAliasKeyword({'shield'})
keywordHandler:addKeyword({'embrace'}, StdModule.say, {npcHandler = npcHandler, text = 'The druids north of Carlin will provide you with the embrace of Tibia.'})
keywordHandler:addKeyword({'suns'}, StdModule.say, {npcHandler = npcHandler, text = 'You can ask for the blessing of the two suns in the suntower near Ab\'Dendriel.'})
keywordHandler:addAliasKeyword({'fire'})
keywordHandler:addKeyword({'phoenix'}, StdModule.say, {npcHandler = npcHandler, text = 'The spark of the phoenix is given by the dwarven priests of earth and fire in Kazordoon.'})
keywordHandler:addAliasKeyword({'spark'})
keywordHandler:addKeyword({'solitude'}, StdModule.say, {npcHandler = npcHandler, text = 'Talk to the hermit Eremo on the isle of Cormaya about this blessing.'})
keywordHandler:addAliasKeyword({'wisdom'})

npcHandler:setMessage(MESSAGE_GREET, 'Welcome, young |PLAYERNAME|! If you are heavily wounded or poisoned, I can {heal} you for free or you want the {adventurer stone}?')
npcHandler:setMessage(MESSAGE_WALKAWAY, 'Remember: If you are heavily wounded or poisoned, I can heal you for free.')
npcHandler:setMessage(MESSAGE_FAREWELL, 'May the gods bless you, |PLAYERNAME|!')

npcHandler:addModule(FocusModule:new())
 
You should provide the error or outcome if any, I didn't bother with your other code because you are trying to see if the callback function is working and this modified version of your callback will determine just that.
Lua:
local function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        -- npc does not have your attention
        print("npc does not have focus")
        return false
    end
    local player = Player(cid)
    -- check for the player
    if player then
        -- now you can get the player's level
        local totalBlessPrice = getBlessingsCost(player:getLevel()) * 5 * 1.3
        if msgcontains(msg, "myass") then
            npcHandler:say("Do you want to receive all five available blessings - for " .. totalBlessPrice .. " gold?", cid)
            npcHandler.topic[cid] = 7
        -- check for the response and the state
        elseif msgcontains(msg, "yes") and npcHandler.topic[cid] == 7 then
            if player:getBlessings() == 5 then
                npcHandler:say("You already have been blessed!", cid)
            elseif player:removeMoneyNpc(totalBlessPrice) then
                -- 1-5 not 2-6
                for b = 1, 5 do
                    -- there is only 1 argument for this method
                    player:addBlessing(b)
                end
                -- make sure the blessings were added
                if player:getBlessings() == 5 then
                    npcHandler:say("You have been blessed by all of five gods!, |PLAYERNAME|.", cid)
                    player:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
                else
                    -- since they weren't added check how many blessings the player has
                    npcHandler:say("blessings count "..player:getBlessings(), cid)
                    player:getPosition():sendMagicEffect(CONST_ME_POFF)
                    return false
                end
            else
                npcHandler:say("Come back when you have enough money.", cid)
            end
        end
    else
        -- player wasn't created
        print("player doesn't exist")
    end
    return true
end

If the script isn't executing at all then you most likely have a clerical error rather than a syntax error.
 
There is no error nor outcome. Noc simply doesnt answer yo that words, only the ones added by keywordhandler. The same piece of code is working on another NPC(i cant post here complete Henrius npc because of lengh)

Looks like never goes into callback
 
Last edited:
Back
Top