Hello everyone. I'm tired of all the single buying commands as "!buyaol". So I've scripted an advanced one that supports backpack and multiple item buying.
It works like this:
Here is the code:
Usage Tutorial:
There you go.
(Report all errors if you find any)
If you find this useful, rep++ me.
It works like this:
Code:
!buy 3 magic swords -- To buy 3 magic swords.
!buy magic sword -- To buy a single magic sword.
!buy bpsd -- To buy a backpack of sds.
!buy info -- Will return a list of all items you can buy with the command.
Here is the code:
Lua:
local items = {
{1, 6500, 10000}, -- Demonic Essence example.
{1, 2463, 600}, -- Plate Armor example.
{1, 2173, 10000, false, 2}, -- Amulet of loss example with 2 charges each amulet.
{2, 1998, "backpack of demonic essences;bpde", 20, 6500, 1, 10000} -- Backpack with demonic essence example.
}
function onSay(cid, words, param)
par = string.explode(param, " ")
if param == "" then
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Please write what item you wish to buy.")
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
return true
end
if param == "info" then
if items == {} then
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "There are no buyable items.")
return true
end
local text = "List of Items:\n"
for i, lineItem in ipairs(items) do
local first_word = string.gsub(getItemNameById(lineItem[2]), "%a", string.upper, 1)
if lineItem[1] == 1 then
text = text .. first_word .. " - " .. lineItem[3] .. " gp\n"
elseif lineItem[1] == 2 then
text = text .. first_word .. " of " .. lineItem[6] * lineItem[4] .. " " .. getItemNameById(lineItem[5]) .. "s - " .. lineItem[6] * lineItem[4] * lineItem[7] .. "gp\n"
end
end
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, text)
return true
end
for i, item in ipairs(items) do
par = getFullParam(par)
if item[1] == 1 then
titem = par[1]
if isNumber(par[1]) == true then
titem = par[2]
if tonumber(par[1]) > 100 or tonumber(par[1]) == 0 then
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
return true
end
end
count = 1
if titem == par[2] then
count = tonumber(par[1])
end
if count > 1 then
_, _, string_enter = string.find(titem, '(.*)s')
if getItemIdByName(string_enter, false) ~= false then
titem = string_enter
elseif getItemIdByName(titem, false) ~= false then
titem = titem
elseif titem == getItemDescriptionsById(item[2]).plural then
titem = getItemNameById(item[2])
else
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
return true
end
end
if titem == getItemNameById(item[2]) then
if doPlayerRemoveMoney(cid, item[3]) == true then
local ret_count = count
if item[4] == false then
ret_count = 1
end
if item[5] == nil then
sItem = doPlayerAddItem(cid, item[2], ret_count)
if (getThing(sItem).type == 0 or item[4] == false) and count > 1 then
for i = 1, count - 1 do
doPlayerAddItem(cid, item[2], 1)
end
end
else
for i = 1, count do
sItem = doPlayerAddItem(cid, item[2], item[5])
end
end
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
if tonumber(count) == 1 then
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have succefully bought ".. getItemDescriptions(sItem).article .." ".. getItemDescriptions(sItem).name ..". ".. getItemDescriptions(sItem).special)
else
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have succefully bought ".. count .." ".. getItemDescriptions(sItem).name .."s. ".. getItemDescriptions(sItem).special)
end
return true
else
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You don\'t have enough money to buy that.")
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
return true
end
end
elseif item[1] == 2 then
ItemName = string.explode(item[3], ";")
for i = 1, #ItemName do
if par[1] == ItemName[i] then
if doPlayerRemoveMoney(cid, item[7]*item[4]) == true then
itemSelect = doPlayerAddItem(cid, item[2], 1)
for s = 1, item[4] do
doAddContainerItem(itemSelect, item[5], item[6])
end
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have succefully bought ".. getItemDescriptions(itemSelect).article .." ".. getItemDescriptions(itemSelect).name .." with ".. item[4]*item[6] .." ".. getItemNameById(item[5]) .."s.")
return true
else
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You don\'t have enough money to buy that.")
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
return true
end
end
end
end
if i == #items then
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You can\'t buy that.")
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_POFF)
return true
end
end
return true
end
function getFullParam(param)
local s = ""
if isNumber(param[1]) == FALSE then
for i = 1, #param do
if i ~= #param then
s = s .. param[i] .. " "
else
s = s .. param[i]
end
end
return {s}
else
for i = 2, #param do
if i ~= #param then
s = s .. param[i] .. " "
else
s = s .. param[i]
end
end
return {param[1], s}
end
end
Usage Tutorial:
Code:
This script supports 2 different kinds of itemtypes, containers and single items.
Both type of items can be bought multiple times.
Type number 1 table(Single items): {type, itemid, price, item-multiplication, charges per item}
- type: This is the type of the item, it's 1 because it's a single item.
- itemid: The itemid of the item. This is everything needed to let the script recognize the item the player wants.
- price: This is the price for each item.
- item-multiplication: This feature is very important for items such as amulets and rings. It will let the player buy many items instead of 1, depending on how many items the player wish to buy. It can only be false(enabled) or true(disabled). This feature is optional.
- charges per item: This is how many charges each item will have, it only works when the item-multiplication feature is enabled.
Type number 2 table(Containers): {item type, container id, item names, items, itemid, charges, price per item}
- type: This is the type of the item, it's 2 because it's a container.
- container id: The itemid of the container.
- item names: Keywords for this container item. Every keyword is separated with a ";".
- items: This is how many items the container should have inside.
- itemid: This is the the itemid for the items inside.
- charges: How many charges each item should have.
- price per item: The price for each item inside the container.
If you don't understand the usage tutorial, please look at the examples I gave in the script.
For a better grammatical correction you can always set a plural name for the items in items.xml.
There you go.
(Report all errors if you find any)
If you find this useful, rep++ me.
Last edited: