• 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 Optimize code - Action

Karofel

New Member
Joined
Apr 12, 2017
Messages
10
Solutions
1
Reaction score
2
Hello, I wrote that script and now want to extend it, but i need to create so much tables etc for that. So there is someone who have idea how to optimize that?
Lua:
local chance1 = 1
local chance2 = 5
local chancerare = 20
local itemstodestroy = {25522, 2494, 25523}
local mats = {5887, 5888}
local raremats = {6500, 12505}
local count = 1

function onUse(cid, item, fromPosition, itemEx, toPosition)
local player = Player(cid)
    local it = itemEx:getId()
    local name = itemEx:getName()
    if isInArray(itemstodestroy, it) then
        count = 5
        if math.random(1, chance1) == 1 then
            player:addItem(mats[math.random(1, #mats)], (math.random(1, count)))
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You succesfull disassembled "..name..".")
            itemEx:remove(1)
            if math.random(1, chance2) == 1 then
                player:addItem(mats[math.random(1, #mats)], (math.random(1, count)))
            elseif math.random(1, chancerare) == 1 then
                player:addItem(raremats[math.random(1, #raremats)], (math.random(1, count)))
                player:sendTextMessage(MESSAGE_INFO_DESCR, "... and get rare mats!")
            end
        else
            player:sendTextMessage(MESSAGE_INFO_DESCR, "You failed on disassembling "..name.."...")
            itemEx:remove(1)
        end
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot disassemble "..name..".")
    end
    return true
end
 
Lua:
local chance1 = 1
local chance2 = 5
local chancerare = 20
local itemstodestroy = {25522, 2494, 25523}
local mats = {5887, 5888}
local raremats = {6500, 12505}
local count = 5
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    local it = itemEx:getId()
    local name = itemEx:getName()

    if not isInArray(itemstodestroy, it) then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You cannot disassemble "..name..".")
        return true -- false?
    end

    itemEx:remove(1)

    if math.random(1, chance1) != 1 then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You failed on disassembling "..name.."...")
        return true -- false?
    end

    player:addItem(mats[math.random(1, #mats)], (math.random(1, count)))
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You succesfull disassembled "..name..".")

    if math.random(1, chance2) == 1 then
        player:addItem(mats[math.random(1, #mats)], (math.random(1, count)))
    elseif math.random(1, chancerare) == 1 then
        player:addItem(raremats[math.random(1, #raremats)], (math.random(1, count)))
        player:sendTextMessage(MESSAGE_INFO_DESCR, "... and get rare mats!")
    end
    return true
end

Not much to optimize here, sorry. Just some clarity added. I think you should also change the way you work with "chance" here to make it easier to read.
 
Back
Top