• 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!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

TalkAction Advanced buying command

Mazen

Developer
Joined
Aug 20, 2007
Messages
612
Reaction score
38
Location
Sweden
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:
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:
Very usefully, I already good a good one, but i'll rep++ you:thumbup:.

Regards,
Shawak
 
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:
Code:
!buy 3, magic sword -- 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}, -- {item type, item id, price} there are two item types, single items(1) and containers(2).
    {2, 1998, "backpack of demonic essences;bpde", 20, 6500, 1, 10000} -- {item type, container id, item names, items, items id, charges, price per item}
}

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, "You cannot buy any 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
        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
            if titem == getItemNameById(item[2]) then
                count = 1
                if titem == par[2] then
                    count = par[1]
                end
                if doPlayerRemoveMoney(cid, item[3]) == true then
                    sItem = doPlayerAddItem(cid, item[2], count)
                    doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
                    doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have succefully bought ".. getItemDescriptions(sItem).article .." ".. getItemDescriptions(sItem).name ..". ".. getItemDescriptions(sItem).special)
					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

There you go.

(Report all errors if you find any)

If you find this useful, rep++ me. ;)

where it paste plx help rep
 
local items = {
{1, 6500, 10000}, -- {item type, item id, price} there are two item types, single items(1) and containers(2).
{2, 1998, "backpack of demonic essences;bpde", 20, 6500, 1, 10000} -- {item type, container id, item names, items, items id, charges, price per item}
}

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, "You cannot buy any 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
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
if titem == getItemNameById(item[2]) then
count = 1
if titem == par[2] then
count = par[1]
end
if doPlayerRemoveMoney(cid, item[3]) == true then
sItem = doPlayerAddItem(cid, item[2], count)
doSendMagicEffect(getCreaturePosition(cid), CONST_ME_MAGIC_RED)
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "You have succefully bought ".. getItemDescriptions(sItem).article .." ".. getItemDescriptions(sItem).name ..". ".. getItemDescriptions(sItem).special)
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 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

name folder for it?? where this paste?
 
@up, go to data/talkactions/scripts/ and create a file called "buy.lua".

In talkactions.xml add:
Code:
	<talkaction words="!buy" filter="word" event="script" value="buy.lua"/>

And it's done.

work with tfs 0.2.2?

I'm sorry it doesn't. :/

Thanks all for the rep. :D
 
Last edited:
I think it's better:
Code:
local items = {
	["crystal coin"] = {id = 2148, count = 1, cost = 10000},
	["magic sword"] = {id = 2400, count = 1, cost = 1000000}
}

local item = items[param]
if (item == nil) then
	return
end

if (getPlayerMoney(cid) >= item.cost) then
	doPlayerAddItem(cid, item.id, item.count)
	doPlayerRemoveMoney(cid, item.cost)
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGe, "You have bought ".. getItemNameById(item.id) ..".")
end

Anyway nice script ;p
 
@Up, sure. But some people maybe prefer something with more features and a more customizable script.

It doesn't have to be that basic. ;)
 
Untested

Lua:
function onSay(cid, words, param, channel)

local items =
{
	["magic sword"] = {itemId = 2400, cost = 150000},
	["soft boots"] = {itemId = 6132, cost = 300000}
}

local tmp = string.explode(param, ",")
local count = tmp[2]
local item = items[tmp[1]]

	if item then

		if(not isNumber(count) or tmp[2] == nil or tmp[2] == 0) then
			if doPlayerRemoveMoney(cid, item.cost) then
				doPlayerGiveItem(cid, item.itemId, 1, 0)
				doPlayerSendTextMessage(cid, MESSAGE_INFO_DECR, "Bought 1x " .. getItemNameById(item.itemId) .. ".")
				return TRUE
			else
				doPlayerSendCancel(cid, "You need " .. item.cost .. " gold pieces to buy this item.")
				return TRUE
			end
		end

		if doPlayerRemoveMoney(cid, count * item.cost) then
			doPlayerGiveItem(cid, item.itemId, count, 0)
			doPlayerSendTextMessage(cid, MESSAGE_INFO_DECR, "Bought " .. count .. "x " .. getItemNameById(item.itemId) .. ".")
			return TRUE
		else
			doPlayerSendCancel(cid, "You need " .. count * item.cost .. " gold pieces to buy this item.")
			return TRUE
		end
	end
	return TRUE
end

* You can use !buy soft boots, 2 <-- buy 2 soft boots.
 
Back
Top