SixNine
Active Member
- Joined
- Dec 12, 2018
- Messages
- 452
- Reaction score
- 41
Hey trying to create NPC that basically he wont be able to type Trade to him until he brings the required stuff, once he brings it he can see what he sells. Tried this code but didnt worked at all
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 shopModule = ShopModule:new()
npcHandler:addModule(shopModule)
shopModule:addBuyableItem({'secret chest'}, 19105, 1000000, 1, 'secret chest')
--The rest of items
local config = {
minLevel = 50,
needItems = {
{id = 1234, count = 5},
{id = 5678, count = 3},
},
}
function creatureSayCallback(cid, type, msg)
local player = Player(cid)
if not npcHandler:isFocused(cid) then
if msg == "hi" or msg == "hello" then
npcHandler:addFocus(cid)
npcHandler:say("Hey, I'm the owner, and I can trade you some furniture. Are you interested in a trade?", cid)
npcHandler.topic[cid] = 1
else
return false
end
elseif msgcontains(msg, "trade") and npcHandler.topic[cid] == 1 then
npcHandler:say("Great! Please type 'items' to see the list of items I need.", cid)
npcHandler.topic[cid] = 2
elseif msgcontains(msg, "items") and npcHandler.topic[cid] == 2 then
local itemsNeeded = {}
for _, v in ipairs(config.needItems) do
local info = ItemType(v.id)
table.insert(itemsNeeded, (v.count > 1 and v.count or info:getArticle()) .. " " .. (v.count > 1 and info:getPluralName() or info:getName()))
end
local neededItemsString = table.concat(itemsNeeded, ", ")
npcHandler:say(string.format("Do you have the following items: %s?", neededItemsString), cid)
npcHandler.topic[cid] = 3
elseif msgcontains(msg, "yes") and npcHandler.topic[cid] == 3 then
local amount = 0
local items = {}
for _, v in ipairs(config.needItems) do
local playerCount = player:getItemCount(v.id)
if playerCount >= v.count then
amount = amount + 1
else
local info = ItemType(v.id)
table.insert(items, (v.count > 1 and v.count or info:getArticle()) .. " " .. (v.count > 1 and info:getPluralName() or info:getName()))
end
end
if amount ~= #config.needItems then
local needItems = table.concat(items, ", ")
npcHandler:say(string.format("Sorry, you don't have the required items: %s.", needItems), cid)
npcHandler.topic[cid] = 0
npcHandler:releaseFocus(cid)
return true
end
for _, v in ipairs(config.needItems) do
player:removeItem(v.id, v.count)
end
npcHandler:say("Thank you for the items! You can now see the list of items I sell.", cid)
player:setStorageValue(123, 1)
npcHandler.topic[cid] = 0
npcHandler:releaseFocus(cid)
elseif msgcontains(msg, "no") and npcHandler.topic[cid] == 3 then
npcHandler:say("No problem! If you change your mind, feel free to come back.", cid)
npcHandler.topic[cid] = 0
npcHandler:releaseFocus(cid)
elseif msgcontains(msg, "bye") then
npcHandler:say("Goodbye!", cid)
npcHandler:releaseFocus(cid)
end
return true
end
npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())