• 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 sell each time other mushrooms

Katrx

New Member
Joined
Jun 20, 2017
Messages
4
Reaction score
0
Hello
Is there anyone here who could help me in edit NPC to sell random mushrooms?
I tried to edit the Santa Claus script but nothing came out of it

Code:
dofile(getDataDir() .. 'npc/scripts/lib/greeting.lua')

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

-- OTServ event handling functions
function onCreatureAppear(cid)            npcHandler:eek:nCreatureAppear(cid)            end
function onCreatureDisappear(cid)        npcHandler:eek:nCreatureDisappear(cid)            end
function onCreatureSay(cid, type, msg)        npcHandler:eek:nCreatureSay(cid, type, msg)        end
function onThink()                npcHandler:eek:nThink()                    end

local shopModule = ShopModule:new()
npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'white mushroom'}, 2787, 10)
shopModule:addBuyableItem({'red mushroom'}, 2788, 10)
shopModule:addBuyableItem({'brown mushroom'}, 2789, 10)
shopModule:addBuyableItem({'orange mushroom'}, 2790, 10)
shopModule:addBuyableItem({'wood mushroom'}, 2791, 10)
shopModule:addBuyableItem({'dark mushroom'}, 2792, 10)
shopModule:addBuyableItem({'some mushroom'}, 2793, 10)
shopModule:addBuyableItem({'some mushroom'}, 2794, 10)
shopModule:addBuyableItem({'fire mushroom'}, 2795, 10)
shopModule:addBuyableItem({'green mushroom'}, 2796, 10)
 
keywordHandler:addKeyword({'time'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = "Watch the sky, it will tell you."})

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
You can try this out. It's a bit hackish

Lua:
dofile(getDataDir() .. 'npc/scripts/lib/greeting.lua')

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

-- OTServ event handling functions
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 shopModule = ShopModule:new()

local meta = getmetatable(shopModule)
local onConfirm = meta.onConfirm
function meta.onConfirm(cid, message, keywords, parameters, node)
    if shop_itemid[cid] == 0 then
        local items = {}
        for i = 1, #npcHandler.shopItems do
            local shopItem = npcHandler.shopItems[i]
            if shopItem.id ~= 0 then
                items[#items + 1] = shopItem.id
            end
        end
        shop_itemid[cid] = items[math.random(#items)]
    end

    return onConfirm(cid, message, keywords, parameters, node)
end

npcHandler:addModule(shopModule)

shopModule:addBuyableItem({'white mushroom'}, 2787, 10)
shopModule:addBuyableItem({'red mushroom'}, 2788, 10)
shopModule:addBuyableItem({'brown mushroom'}, 2789, 10)
shopModule:addBuyableItem({'orange mushroom'}, 2790, 10)
shopModule:addBuyableItem({'wood mushroom'}, 2791, 10)
shopModule:addBuyableItem({'dark mushroom'}, 2792, 10)
shopModule:addBuyableItem({'some mushroom'}, 2793, 10)
shopModule:addBuyableItem({'some mushroom'}, 2794, 10)
shopModule:addBuyableItem({'fire mushroom'}, 2795, 10)
shopModule:addBuyableItem({'green mushroom'}, 2796, 10)
shopModule:addBuyableItem({'food'}, 0, 10, nil, 'random mushroom')
shopModule:addBuyableItem({'mushroom'}, 0, 10, nil, 'random mushroom')

keywordHandler:addKeyword({'time'}, StdModule.say, {npcHandler = npcHandler, onlyFocus = true, text = "Watch the sky, it will tell you."})

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
You can try this out. It's a bit hackish
12:56 Anerui: Do you want to buy random mushroom for 10 gold coins?

After writing "yes" the NPC does not respond and the console pops errors

Code:
Lua Script Error: [Npc interface]
data/npc/scripts/Anerui.lua:onCreatureSay
data/npc/scripts/Anerui.lua:18: attempt to index global 'shop_itemid' (a nil value)
stack traceback:
        data/npc/scripts/Anerui.lua:18: in function 'callback'
        data/npc/scripts/lib/npcsystem/keywordhandler.lua:47: in function 'processMessage'
        data/npc/scripts/lib/npcsystem/keywordhandler.lua:180: in function 'processNodeMessage'
        data/npc/scripts/lib/npcsystem/keywordhandler.lua:133: in function 'processMessage'
        data/npc/scripts/lib/npcsystem/npchandler.lua:298: in function 'onCreatureSay'
        data/npc/scripts/Anerui.lua:10: in function <data/npc/scripts/Anerui.lua:10>

keywordhandler.lua:47
Code:
return (self.callback == nil or self.callback(cid, message, self.keywords, self.parameters, self))
keywordhandler.lua:180
Code:
if(childNode:processMessage(cid, message)) then
keywordhandler.lua:133
Code:
local ret = self:processNodeMessage(node, cid, message)
keywordhandler.lua - Pastebin.com

npchandler.lua:298
Code:
local ret = self.keywordHandler:processMessage(cid, msg)
npchandler.lua - Pastebin.com
 
If he's buying the mushroom via text.. why not just use a table?
Code:
local mushrooms ={1111, 2222, 3333}
local rand = math.random(#mushrooms)
doPlayerAddItem(cid, mushrooms[rand], 1)
 
@Xikini Could you help with this code?
After pasting I get something like this
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/Anerui.lua
LuaScriptInterface::luaDoPlayerAddItem(). Player not found

Code:
dofile(getDataDir() .. 'npc/scripts/lib/greeting.lua')

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

-- OTServ event handling functions
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 shopModule = ShopModule:new()

local mushrooms ={2787, 2788, 2789, 2790, 2791, 2792, 2793, 2794, 2795, 2796}
local rand = math.random(#mushrooms)
doPlayerAddItem(cid, mushrooms[rand], 1)

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
 
@Xikini Could you help with this code?
After pasting I get something like this
Code:
Lua Script Error: [Npc interface]
data/npc/scripts/Anerui.lua
LuaScriptInterface::luaDoPlayerAddItem(). Player not found

Code:
dofile(getDataDir() .. 'npc/scripts/lib/greeting.lua')

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

-- OTServ event handling functions
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 shopModule = ShopModule:new()

local mushrooms ={2787, 2788, 2789, 2790, 2791, 2792, 2793, 2794, 2795, 2796}
local rand = math.random(#mushrooms)
doPlayerAddItem(cid, mushrooms[rand], 1)

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())
It's generally not a good practice to tag someone (@Katrx) directly. :p

@topic, The server doesn't know who cid is, because you never tell the npc who it's talking to.

I'll post an npc script I use on my TFS 0.3.7 server.
Hopefully it works for you.

https://otland.net/threads/Simple_Base_NPC_0_3_7.241103/#post-2331741

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
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 the world how fashionable they can be!", cid)

    elseif msgcontains(msg, "name") then
        selfSay("My name is " .. getNpcName() .. ".", cid)

    elseif msgcontains(msg, "mushroom") then
        local mushrooms = {2787, 2788, 2789, 2790, 2791, 2792, 2793, 2794, 2795, 2796}
        local rand = math.random(#mushrooms)
        doPlayerAddItem(cid, mushrooms[rand], 1)
        selfSay("You have been given a random mushoom!", cid)

    end
    return true
end

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