• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Lua Storage Npc

DiegoRulez

Member
Joined
Apr 16, 2012
Messages
99
Reaction score
13
Location
Feira de Santana - Brasil
I'm using scripts for NPCs based on tutorial Limos.
https://otland.net/threads/npc-mission.211063/

Code:
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 = 55201

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

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

     if msgcontains(msg, "word of greeting") then
         if getPlayerStorageValue(cid, storage) == -1 then
             selfSay("Talk 'yes' to continue", cid)
             talkState[talkUser] = 1
         elseif getPlayerStorageValue(cid, storage) == 1 then
             selfSay("Talk 'yes' to continue", cid)
             talkState[talkUser] = 1
         else
             selfSay("You already have my permission. Proceed with your mission", cid)
         end
     elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
         if getPlayerStorageValue(cid, storage) == -1 then
             selfSay("", cid)
             setPlayerStorageValue(cid, storage, 1)
         else
             if(doPlayerRemoveItem(cid, 2148, 1)) then 
                 selfSay("Ready! You have my permission.", cid)
                 setPlayerStorageValue(cid, 55101, 1)
                 setPlayerStorageValue(cid, storage, 2)
             else
                 selfSay("You do not have 1 Gold Coin to buy my permission .", cid)
             end
         end
         talkState[talkUser] = 0
     elseif msgcontains(msg, "no") and talkState[talkUser] == 1 then
         selfSay("Ok then.", cid)
         talkState[talkUser] = 0
     end
     return true
end

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


How do you already have to have a storage to talk to this NPC ?

and..

How can I make it delivered to storage without giving anything in return ?
 
How do you already have to have a storage to talk to this NPC ?

and..

How can I make it delivered to storage without giving anything in return ?
Do you mean the NPC should refuse to talk to a player if they don't have a specific storage value?

and..

I'm not entirely sure what you want here.

Can you write your requests/issues in your native language, then use google translate?
 
Sorry, I got left alone at work tonight, so had no time to grab a break until someone came in.

-- Edit --
Both scripts untested.
- - - - - - - - - - -
For the first part you can do a npc greet callback.

Code:
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

function greet(cid)
    if getPlayerStorageValue(cid, 45001) < 1 then
        selfSay("Quest not completed. Please complete the quest to speak", cid)
        npcHandler:releaseFocus(cid)
    end
    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

    if msgcontains(msg, "job") then
        selfSay("My job is to show you a basic npc file!", cid)
    elseif msgcontains(msg, "name") then
        selfSay("My name is " .. getNpcName() .. ".", cid)

    -- talkState[talkUser] = 0

    end

    return true
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
And for the second one, you just give the storage value as the reward.
It just seems a little simple though?
Maybe I'm not understanding you correctly.
Code:
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

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

    if msgcontains(msg, "job") then
        selfSay("My job is to show you a basic npc file!", cid)
    elseif msgcontains(msg, "name") then
        selfSay("My name is " .. getNpcName() .. ".", cid)

    elseif msgcontains(msg, "give storage") then
        if getPlayerStorageValue(cid, 45001) == -1 then
            selfSay("Storage given.", cid)
            setPlayerStorageValue(cid, 45001, 1)
        else
            selfSay("Storage given previously.", cid)
        end


    -- talkState[talkUser] = 0

    end

    return true
end

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

Welp, guess that's it.

Cheers,

Xikini
 
Last edited by a moderator:
Here, try this for the first npc instead.

-- the first part with the bye/goodbye is probably unnecessary, but I added it anyways
Code:
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

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
   
     if msgcontains(msg, "bye") or msgcontains(msg, "goodbye") then
         selfSay("Good bye, " .. getCreatureName(cid) .. "!", cid)
         npcHandler:releaseFocus(cid)
         return true
     elseif getPlayerStorageValue(cid, 45001) < 1 then
         selfSay("Sorry, I'm busy.", cid)
         return true
   
     elseif msgcontains(msg, "job") then
         selfSay("My job is to show you a basic npc file!", cid)
     elseif msgcontains(msg, "name") then
         selfSay("My name is " .. getNpcName() .. ".", cid) 
   
     -- talkState[talkUser] = 0
   
     end
   
     return true
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
The two NPC 's are functional. Thanks.

--

Could add the function : " bring me to [city ]" to transport those who are not in conversation with the NPC ?

Code:
local keywordHandler = KeywordHandler:new()
        local npcHandler = NpcHandler:new(keywordHandler)
        NpcSystem.parseParameters(npcHandler)
      
      
      
        -- OTServ event handling functions start
        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
        -- OTServ event handling functions end
      
      
        -- Don't forget npcHandler = npcHandler in the parameters. It is required for all StdModule functions!
    local travelNode = keywordHandler:addKeyword({'ab\'dendriel'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Ab\'dendriel for 130 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 130, destination = {x=32734, y=31668, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})

    local travelNode = keywordHandler:addKeyword({'carlin'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Carlin for 110 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 110, destination = {x=32387, y=31820, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})
      
    local travelNode = keywordHandler:addKeyword({'edron'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Edron for 160 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 160, destination = {x=33173, y=31764, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})
          
    local travelNode = keywordHandler:addKeyword({'venore'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Venore for 170 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 170, destination = {x=32954, y=32022, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})
      
    local travelNode = keywordHandler:addKeyword({'port hope'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Port Hope for 160 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 160, destination = {x=32527, y=32784, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})

        keywordHandler:addKeyword({'travel'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Where do you want to go? To Ab\'Dendriel, Carlin, Edron, Venore and Port Hope.'})
        keywordHandler:addKeyword({'help'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Where do you want to go? To Ab\'Dendriel, Carlin, Edron, Venore and Port Hope.'})
     

        npcHandler:addModule(FocusModule:new())

EDIT

Code:
-- Town name, Price, x.pos, y.pox, z.pos --
local config = {
   ["Dreams City"] = {cost = 100, location = {x = 1000, y = 1000, z = 7}},
   ["Put a city name"] = {cost = 100, location = {x = 0000, y = 0000, z = 0}}
}

local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)
local talkState, xmsg = {}, {}
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
function creatureSayCallback(cid, type, msg)
    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
     local tm = config[msg:lower()]
     if msgcontains(msg, "bring me to") then
         for mes, t in pairs(config) do
             if msgcontains(msg, mes) then
                if isPlayerPzLocked(cid) == true then
                    doPlayerSendTextMessage(cid,MESSAGE_STATUS_SMALL,"Those hands look pretty bloody")
                    npcHandler:say("Hah, Seems like you got blood on your hands you shall not sail with me.", cid)
                    return false
                end
                    if doPlayerRemoveMoney(cid, t.cost) then
                        npcHandler:say("You paid ".. t.cost .." gold coins to travel and you have ".. getPlayerMoney(cid) .." gold coins left in your backpack", cid)
                        doTeleportThing(cid, t.location)
                    else
                     npcHandler:say("I am sorry you have ".. getPlayerMoney(cid) .." gold coins and it costs ".. t.cost .." gold coins to travel.", cid)
                end
             end
         end      
     elseif tm then
         if getPlayerMoney(cid) >= tm.cost then
             npcHandler:say("Do you want to go to ".. msg .." for ".. tm.cost .." gold coins? ... you do have ".. getPlayerMoney(cid) .." gold coins!", cid)
             talkState[talkUser] = 1
             xmsg[cid] = msg
         else
             npcHandler:say("I am sorry you have ".. getPlayerMoney(cid) .." gold coins and it t.cost's ".. tm.cost .." gold coins to travel.", cid)
         end
     elseif msgcontains(msg, "yes") and talkState[talkUser] == 1 then
         local tm = config[xmsg[cid]:lower()]
                if isPlayerPzLocked(cid) == true then
                    doPlayerSendTextMessage(cid,MESSAGE_STATUS_SMALL,"Those hands look pretty bloody")
                    npcHandler:say("Hah, Seems like you got blood on your hands you shall not sail with me.", cid)
                    return false
                end
         if doPlayerRemoveMoney(cid, tm.cost) == true then
             npcHandler:say("You paid ".. tm.cost .." gold coins to travel and you have ".. getPlayerMoney(cid) .." gold coins left in your backpack", cid)
             doTeleportThing(cid, tm.location)
         else
             npcHandler:say("I am sorry you have ".. getPlayerMoney(cid) .." gold coins and it t.cost's ".. cost[cid] .." gold coins to travel.", cid)
         end
         talkState[talkUser] = 0
     elseif msgcontains(msg, "no") and talkState[talkUser] > 0 then
         npcHandler:say("Ok.", cid)
         talkState[talkUser] = 0
     end
     return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Last edited:
The two NPC 's are functional. Thanks.

--

Could add the function : " bring me to [city ]" to transport those who are not in conversation with the NPC ?

Code:
local keywordHandler = KeywordHandler:new()
        local npcHandler = NpcHandler:new(keywordHandler)
        NpcSystem.parseParameters(npcHandler)
    
    
    
        -- OTServ event handling functions start
        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
        -- OTServ event handling functions end
    
    
        -- Don't forget npcHandler = npcHandler in the parameters. It is required for all StdModule functions!
    local travelNode = keywordHandler:addKeyword({'ab\'dendriel'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Ab\'dendriel for 130 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 130, destination = {x=32734, y=31668, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})

    local travelNode = keywordHandler:addKeyword({'carlin'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Carlin for 110 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 110, destination = {x=32387, y=31820, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})
    
    local travelNode = keywordHandler:addKeyword({'edron'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Edron for 160 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 160, destination = {x=33173, y=31764, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})
        
    local travelNode = keywordHandler:addKeyword({'venore'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Venore for 170 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 170, destination = {x=32954, y=32022, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})
    
    local travelNode = keywordHandler:addKeyword({'port hope'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Do you want to sail to Port Hope for 160 gps?'})
            travelNode:addChildKeyword({'yes'}, StdModule.travel, {npcHandler = npcHandler, premium = false, level = 0, cost = 160, destination = {x=32527, y=32784, z=6} })
            travelNode:addChildKeyword({'no'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, reset = true, text = 'We would like to serve you some time.'})

        keywordHandler:addKeyword({'travel'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Where do you want to go? To Ab\'Dendriel, Carlin, Edron, Venore and Port Hope.'})
        keywordHandler:addKeyword({'help'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = 'Where do you want to go? To Ab\'Dendriel, Carlin, Edron, Venore and Port Hope.'})
   

        npcHandler:addModule(FocusModule:new())
That's an interesting one.
I'll give it a shot.

I'm guessing this isn't the best way to do it, but I have no idea how else to do it.
Mediocre scripter here.

Basically player says "bring me to carlin" and if carlin is a city, and the player has not stated more then 1 city, like.. "bring me to carlin venore edron" (this won't work..), and the player has enough money, the npc will remove the money required from the player, and then teleport them.

All of above only works if the player is not engaged with the npc directly. (aka; never said hi/hello)

Untested.
Code:
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

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

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

local cities = {
   [1] = {city = "ab'dendriel", price = 130, destination = {x = 32734, y = 31668, z = 6}},
   [2] = {city = "carlin",      price = 110, destination = {x = 32387, y = 31820, z = 6}},
   [3] = {city = "edron",       price = 160, destination = {x = 33173, y = 31764, z = 6}},
   [4] = {city = "venore",      price = 170, destination = {x = 32954, y = 32022, z = 6}},
   [5] = {city = "port hope",   price = 160, destination = {x = 32527, y = 32784, z = 6}}
}
local city_count = 0
local city = 0

function creatureSayCallback(cid, type, msg)
     if(not npcHandler:isFocused(cid)) then
         if msgcontains(msg, "bring me to") then
             for i = 1, #cities do
                 if msgcontains(msg, "" .. cities[i].city .. "") then
                     city_count = city_count + 1
                     city = i
                 end
             end
             if city_count == 1 then
                 if getPlayerMoney(cid) >= cities[city].price then
                     doPlayerRemoveMoney(cid, cities[city].price)
                     doTeleportThing(cid, cities[city].destination)
                     return true
                 end
             end
         end
         return false
     end
   
     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
   
     if msgcontains(msg, "job") then
         selfSay("My job is to show you a basic npc file!", cid)
     elseif msgcontains(msg, "name") then
         selfSay("My name is " .. getNpcName() .. ".", cid) 
   
     -- talkState[talkUser] = 0
   
     end
   
     return true
end

npcHandler:setCallback(CALLBACK_GREET, greet)
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
Back
Top