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

Action Simple Shop Levers

bobzero

LUA Scripter
Joined
Dec 7, 2008
Messages
7
Reaction score
0
Location
Brasil
How it Works?
I'm using TFS 0.3.5 PL1
I got this idea from 0.3 Quest System by Elf

First: You add the item you want to sell with levers at an inacessible place of your map and give to it an uniqueid.

Second: Place the lever and give to it an actionid. You can use the same actionid in more than one lever for selling the same item.

Third: Edit the shoplevers array in the code. It will be explained later.

Fourth: Add the actionid you used in actions.xml, using the filename you gave to te main code file.

Main code. Just save it with the name you want in the action scripts folder. You can also move these first three arrays (LEVERS, LEFT_LEVERS, RIGHT_LEVERS) to constant.lua in lib folder if you want.

Lua:
-- easy shop levers by BobZero
-- with discount system

LEVERS = {1945, 1946, 9825, 9826, 9827, 9828}
LEFT_LEVERS = {1945, 9825, 9827}
RIGHT_LEVERS = {1946, 9826, 9828}

config = {
	onlyLeftToRight = true,
	discountPercent = 10
}

local shoplevers = { -- You should edit this part of the code

	[3100] = {14501, 900, "a backpack of health potions"},
	[3101] = {14502, 1000, "a backpack of mana potions"},
	[3102] = {14503, 2000, "a backpack of strong health potions"},
	[3103] = {14504, 1600, "a backpack of strong mana potions"},
	[3104] = {14505, 3800, "a backpack of great health potions"},
	[3105] = {14506, 2400, "a backpack of great mana potions"},
	[3106] = {14507, 3800, "a backpack of great spirit potions"},
	[3107] = {14508, 6200, "a backpack of ultimate health potions"}
	
}

function onUse(cid, item, fromPosition, itemEx, toPosition)

	if not isInArray(LEVERS, item.itemid) or shoplevers[item.actionid] == nil then
		return false
	end
	
	local itemToBuy = getThing(shoplevers[item.actionid][1])
	
	if itemToBuy == nil then
		return false	
	end

	local newitemid = 0
	
	if isInArray(LEFT_LEVERS, item.itemid) or (isInArray(RIGHT_LEVERS, item.itemid) and not config.onlyLeftToRight) then
	
		local buying = doCopyItem(itemToBuy, false)
		local cost = shoplevers[item.actionid][2]
		cost = cost*(100-config.discountPercent)/100
		local namestr = shoplevers[item.actionid][3]
		local result = ""

		if (getPlayerMoney(cid) < cost) then
			result = "You want to buy " .. namestr ..", that costs " .. cost .. " gold coins, but you have not enough money."
			doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, result)
			return true
		else
			if(doPlayerAddItemEx(cid, buying.uid, false) ~= RETURNVALUE_NOERROR) then
				result = "You have not enough space or capacity for " .. namestr ..", that weighs " .. getItemWeight(buying.uid) .. " oz."
				doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, result)
				return true
			else
				result = "You have bought " .. namestr .. " for " .. cost .. " gold coins."
				doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, result)
				doPlayerAddItem(uid, buying)
				doPlayerRemoveMoney(cid, cost)
			end	
		end

		if isInArray(LEFT_LEVERS, item.itemid) then
			newitemid = item.itemid + 1
		else
			newitemid = item.itemid - 1
		end

	elseif isInArray(RIGHT_LEVERS, item.itemid) then
		newitemid = item.itemid - 1
	end
	
	doTransformItem(item.uid, newitemid)
	return true
	
end

In my example, all levers with actionid = 3100 will sell the item I put in my map with uniqueid = 14501 (In my case it is a backpack filled with health potions) for 900 gold coins less the discount (10 percent, as I set in config, at the beginning of the code).

Line in actions.xml for each used actionid:
Code:
	<action actionid="3100" script="other/shoplevers.lua" />

That's all. Please comment, this is my first published work. More will come if you like this.

PS: please say me if I got terrible english errors, I speak portuguese =D
 
Last edited:
hey I found a bug in the cap 20:53 You have not enough space or capacity for a backpack of health potions, that weighs 000 oz.


I cant but it :/
pliz help xD
 
I just wonder why no one uses doPlayerBuyItemContainer(cid, containerid, itemid, count, cost, charges)...
 
Back
Top