• 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!

Help with shop npc script

tarantonio

Old School Player
Joined
Jun 21, 2009
Messages
868
Solutions
1
Reaction score
286
I want to make an npc that buy/sell items, but I don't want to use npc libs, I want to code the shop into npc lua script.

I need help with the amount of items to buy/sell.

Example:

player: sell 3 serpent swords
npc: do you want to sell 3 serpent swords?

How do I manage the amount (3 in this example) in the lua script?
 
I had trouble with this as well, and opted to ask the player step by step what they wanted to do. :(

Player: serpent sword
NPC: would you like to buy or sell a serpent sword?
Player: sell
NPC: how many would you like to sell?
Player: 3
NPC: Would you like to sell 3 serpent swords for 2550 gold?
Player: yes
NPC: Thank you for your patronage!

-- Edit
and I just made a table of all available items for that specific npc, then they just guide the player through the rest.

I'm only a mediocre scripter though. :/
 
Last edited by a moderator:
Code:
 local sellable_items = {
                ["serpent sword"] = {Money = 1000, ItemId = 9999},
                }

            for v in pairs(sellable_items) do
             if (msgcontains(msg:lower(),'sell')) and (getCount(msg) > 1) and (msgcontains(msg, v)) and (npcHandler.focus == cid) then
                Count = getCount(msg)
                ItemId = sellable_items[v].ItemId
                Money = sellable_items[v].Money
                npcHandler:say('Do you want to sell '..Count..' '..getItemNameById(sellable_items[v].ItemId)..'s for '..sellable_items[v].Money*Count..' gold?')
                npcHandler:doTopic(cid, 2)

             elseif(msgcontains(msg:lower(),'sell')) and (msgcontains(msg, v)) and (npcHandler.focus == cid) then
                Count = 1
                ItemId = sellable_items[v].ItemId
                Money = sellable_items[v].Money
                npcHandler:say('Do you want to sell a '..getItemNameById(sellable_items[v].ItemId)..' for '..sellable_items[v].Money..' gold?')
                npcHandler:doTopic(cid, 2)
             end
            end

             if (msgcontains(msg:lower(),'yes')) and (npcHandler.Topic == 2) and (npcHandler.focus == cid) then
                    if (doPlayerRemoveItem(cid,ItemId,Count)) then
                        doPlayerAddMoney(cid,Count*Money)
                        npcHandler:say('Ok. Here is your gold.')
                    else
                     if  (Count > 1) then
                        npcHandler:say("You don't have that many.")
                     else
                        npcHandler:say("You don't have one.")
                     end
                    end
                end
                        npcHandler:doTopic(cid, 0)
             end

something like that
 
Code:
 local sellable_items = {
                ["serpent sword"] = {Money = 1000, ItemId = 9999},
                }

            for v in pairs(sellable_items) do
             if (msgcontains(msg:lower(),'sell')) and (getCount(msg) > 1) and (msgcontains(msg, v)) and (npcHandler.focus == cid) then
                Count = getCount(msg)
                ItemId = sellable_items[v].ItemId
                Money = sellable_items[v].Money
                npcHandler:say('Do you want to sell '..Count..' '..getItemNameById(sellable_items[v].ItemId)..'s for '..sellable_items[v].Money*Count..' gold?')
                npcHandler:doTopic(cid, 2)

             elseif(msgcontains(msg:lower(),'sell')) and (msgcontains(msg, v)) and (npcHandler.focus == cid) then
                Count = 1
                ItemId = sellable_items[v].ItemId
                Money = sellable_items[v].Money
                npcHandler:say('Do you want to sell a '..getItemNameById(sellable_items[v].ItemId)..' for '..sellable_items[v].Money..' gold?')
                npcHandler:doTopic(cid, 2)
             end
            end

             if (msgcontains(msg:lower(),'yes')) and (npcHandler.Topic == 2) and (npcHandler.focus == cid) then
                    if (doPlayerRemoveItem(cid,ItemId,Count)) then
                        doPlayerAddMoney(cid,Count*Money)
                        npcHandler:say('Ok. Here is your gold.')
                    else
                     if  (Count > 1) then
                        npcHandler:say("You don't have that many.")
                     else
                        npcHandler:say("You don't have one.")
                     end
                    end
                end
                        npcHandler:doTopic(cid, 0)
             end

something like that
I forgot to say that I use OTHire engine, probably I'll have to modify some things to make it work on it.
 
Code:
local string = "sell 3 serpent swords"
print(string:match('%s(%d+)%s'))
the string:match will return the first number found in the string separated by space on both sides, then use normal script with the count

edit: i would not use a script that looks like what ryan posted, having a loop to go through every possible sellable item is a very bad way for performance, rather just set it so for example if you want players to say
"sell N item"
string:match() the 3rd and 4th word and check if its in the table (with or without an extra s at the end e.g serpent swordS)
 
Last edited:
Back
Top