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

Lua How to short this script.

brunolopes

New Member
Joined
Nov 8, 2009
Messages
49
Reaction score
1
Hello, people. ^_^

I've found a script that I made a time ago and I tried to use for and tables to shorten it.
It's a smithing system where you use a hammer in some stacked items to create another item, example: Magic Sword.

I want to use a recipes table and for to short the script, but I'm not able to do that...

Here is the script

Code:
function onUse(cid, item, fromPosition, itemEx, toPosition)
	local positions = {
		item1 = {x = toPosition.x, y = toPosition.y, z = toPosition.z, stackpos = 1},
		item2 = {x = toPosition.x, y = toPosition.y, z = toPosition.z, stackpos = 2},
		item3 = {x = toPosition.x, y = toPosition.y, z = toPosition.z, stackpos = 3},
		item4 = {x = toPosition.x, y = toPosition.y, z = toPosition.z, stackpos = 4},
		item5 = {x = toPosition.x, y = toPosition.y, z = toPosition.z, stackpos = 5}
	}
	
	local item1 = getThingFromPos(positions.item1)
	local item2 = getThingFromPos(positions.item2)
	local item3 = getThingFromPos(positions.item3)
	local item4 = getThingFromPos(positions.item4)
	local item5 = getThingFromPos(positions.item5)
	
	if(item1.itemid == 2157 and item2.itemid == 2147 and item3.itemid == 5468 and item4.itemid == 3276 and item5.itemid == 2160) then
		if(doRemoveItem(item1.uid, 3) and doRemoveItem(item2.uid, 10) and doRemoveItem(item3.uid, 1) and doRemoveItem(item4.uid, 1) and doRemoveItem(item5.uid, 60)) then
			doCreateItem(2400, 1, positions.item1)
		end
	end
	return true
end

Thanks.
 
Last edited:
Lua:
local itanz = {2157, 2147, 5468, 3276, 2160}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local find = {}
    
    for k, v in pairs(itanz) do
        toPosition.stackpos = k
        if (getThingFromPos(toPosition).itemid ~= v) then
            return false
        end
        table.insert(find, getThingFromPos(toPosition).uid)
    end
    
    for _, uid in pairs(find) do
        doRemoveItem(uid)        -- should it have count O_o?
    end
    
    doCreateItem(2400, 1, toPosition)
    return true
end
 
with count.

Lua:
local itanz = {{2157, 3}, {2147, 10}, {5468, 1}, {3276, 1}, {2160, 60}}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local find = {}

    for k, v in pairs(itanz) do
        toPosition.stackpos = k
        if (getThingFromPos(toPosition).itemid ~= v[1]) then
            return false
        end
        table.insert(find, getThingFromPos(toPosition).uid)
    end

    for i, uid in pairs(find) do
        doRemoveItem(uid, itanz[i][2])
    end

    doCreateItem(2400, 1, toPosition)
    return true
end
 
with count.

Lua:
local itanz = {{2157, 3}, {2147, 10}, {5468, 1}, {3276, 1}, {2160, 60}}

function onUse(cid, item, fromPosition, itemEx, toPosition)
    local find = {}

    for k, v in pairs(itanz) do
        toPosition.stackpos = k
        if (getThingFromPos(toPosition).itemid ~= v[1]) then
            return false
        end
        table.insert(find, getThingFromPos(toPosition).uid)
    end

    for i, uid in pairs(find) do
        doRemoveItem(uid, itanz[i][2])
    end

    doCreateItem(2400, 1, toPosition)
    return true
end

u dont even check for count ;/
 
u dont even check for count ;/
he either did, as the tittle says "how to short this script" :p

anyway

Lua:
local itanz = {{2157, 3}, {2147, 10}, {5468, 1}, {3276, 1}, {2160, 60}}
 
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local find = {}

    for k, v in pairs(itanz) do
        toPosition.stackpos = k
        local thing = getThingFromPos(toPosition)
        if (thing.itemid ~= v[1]) or (thing.type < v[2]) then
            return false
        end
        table.insert(find, thing.uid)
    end

    for i, uid in pairs(find) do
        doRemoveItem(uid, itanz[i][2])
    end

    doCreateItem(2400, 1, toPosition)
    return true
end
 
oki
Lua:
local t = {
	[2400] = {
		{2157, 3}, {2147, 10}, {5468, 1}, {2160, 60}
	},
}

function onUse(cid, item, fromPosition, itemEx, toPosition)
	local tmp, match = {}

	for ret, v in pairs(t) do
		for i = 1, #v do
			local g = getTileItemById(toPosition, v[i][1])
			if g.uid > 0 and math.max(1, g.type) >= v[i][2] then
				table.insert(tmp, g.uid)
				if i == #v then
					match = ret
				end
			else
				tmp = {}
				break
			end
		end
		if match then
			break
		end
	end

	if match then
		for i = 1, #tmp do
			doRemoveItem(tmp[i], t[match][i][2])
		end
		doCreateItem(match, 1, toPosition)
		doSendMagicEffect(toPosition, CONST_ME_MAGIC_GREEN)
		return true
	end

end
 
Back
Top