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

Need help with transforming items.

Qbee

Open Tibia Fan
Joined
Mar 9, 2009
Messages
174
Reaction score
4
Hi there. Im learning lua since a couple of days, and i just need a little help out. Let's say i need a script which works like that: When player use item1 on item2, then item2 has 50% chance to transform into item3 and 50% chance to transform into item4. I hope that someone can help me.

Thanks in advance.

@edit

Oh god it's wrong section, im so sorry.
 
Last edited:
Newbie version:
LUA:
-- We'll be using TRUE and FALSE for backwards compatibility with both TFS 0.3 and 0.2
function onUse(cid, item, fromPosition, itemEx, toPosition)
	-- itemEx is the item that we clicked on either with right mouse button or using Use with... (depends on Control settings in client)
	-- Since it's actually a table, we need to call field itemid
	if itemEx.itemid == 2500 then
		-- Found match
		local random = math.random(1, 2) -- Randomizing
		if random == 1 then -- In case the random number is 1
			doTransformItem(itemEx.uid, 2400)
		elseif random == 2 then -- In case it's 2
			doTransformItem(itemEx.uid, 2450)
		end
		return TRUE -- placed it here because item was used on correct itemEx and we don't want 'You cannot use this object.' to popup
	end
	return FALSE -- If we return FALSE or nothing (nil), the player will receive 'You cannot use this object.' message
	-- Notice: FALSE is the same as boolean false in TFS 0.3, and number 0 in TFS 0.2
end
Not readable:
LUA:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local random = math.random(2)
	return itemEx.itemid == 2500 and doTransformItem(itemEx.uid, random == 1 and 2400 or 2450) and TRUE or FALSE
end
The right way:
LUA:
local config = {
	useOnItem = 2500,
	table = {
		{randomRange = {1, 50}, item_id = 2400},
		{randomRange = {51, 100}, item_id = 2450}
	}
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if itemEx.itemid == config.useOnItem then
		local random = math.random(100)
		for _, v in pairs(config.table) do
			if random >= v.randomRange[1] and random <= v.randomRange[2] then
				return TRUE, doTransformItem(itemEx.uid, v.item_id)
			end
		end
	end
	return FALSE
end
more:
LUA:
local config = {
	useOnItem = 2500,
	table = {
		[2400] = {1, 50},
		[2450] = {51, 100}
	}
}
function onUse(cid, item, fromPosition, itemEx, toPosition)
	if itemEx.itemid == config.useOnItem then
		local random = math.random(100)
		for item_id, range in pairs(config.table) do
			if random >= range[1] and random <= range[2] then
				return TRUE, doTransformItem(itemEx.uid, item_id)
			end
		end
	end
	return FALSE
end
 
Last edited:
Back
Top