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

Solved Optimize elseif script.

OTcreator

Active Member
Joined
Feb 14, 2022
Messages
425
Solutions
1
Reaction score
44
Hello.
Who can optimize this script?
I can't add this on CODE becouse have to much characters.
Script in attachment.
 

Attachments

I just tested it, and it worked perfectly! It now accepts both genders, male and female. The post has been corrected, all good!

Lua:
local config = {
    lever = 18563,
    pricePos = { x = 653, y = 734, z = 7 },
    addonName = { "first", "second" },
    outfits = {
        [50002] = {
            items = {
                { id = 5890, count = 100 }
            },
            outfit = {
                [PLAYERSEX_FEMALE] = 136,
                [PLAYERSEX_MALE] = 128,
            },
            addon = 1
        }
       -- Add more outfits as needed
    }
}

local action = Action()

local removePrice = function(tile, items)
    local tileItems = tile:getItems()
    local itemFound = false

    for _, item in ipairs(items) do
        local count = item.count or 1
        itemFound = false

        for _, tileItem in ipairs(tileItems) do
            if tileItem:getId() == item.id and count > 0 then
                local removeCount = math.min(count, tileItem:getCount())
                count = count - removeCount
                tileItem:remove(removeCount)
                itemFound = true
            end
        end

        if not itemFound then
            print("Item not found:", item.id)
            return false
        end
    end

    return true
end

local checkPrice = function(tile, items)
    local countMap = {}
    for _, item in ipairs(items) do
        local tileCount = countMap[item.id] or tile:getItemCountById(item.id)
        local count = item.count or 1

        if tileCount < count then
            return false
        end

        countMap[item.id] = tileCount - count
    end

    return true
end

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.actionid then
        local priceTile = Tile(config.pricePos)
        if not priceTile then
            print("Error: Unable to create priceTile")
            return false
        end

        if not player:isPremium() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Sorry, only premium players may use addon lever.")
            return true
        end

        for sId, o in pairs(config.outfits) do
            local hasOutfit = player:getStorageValue(sId) > 0
            if not hasOutfit and checkPrice(priceTile, o.items) then
                if removePrice(priceTile, o.items) then
                    local lookType = o.outfit[player:getSex()]
                    if lookType then
                        local outfit = Outfit(lookType)
                        outfit.lookAddons = o.addon or 1
                        local addonName = config.addonName[outfit.lookAddons] or ""
                        priceTile:getPosition():sendMagicEffect(CONST_ME_HITBYFIRE)
                        player:setStorageValue(sId, 1)
                        player:addOutfitAddon(lookType, outfit.lookAddons)
                        player:sendTextMessage(MESSAGE_INFO_DESCR, ("You have unlocked the addon %s %s!"):format(addonName, outfit.name))
                        return true
                    end
                else
                    player:say("You don't have the necessary items!", TALKTYPE_ORANGE_1, false, player, priceTile:getPosition())
                    return true
                end
            end
        end

        player:say("You do not have the necessary items or you already have this addon!", TALKTYPE_ORANGE_1, false, player, priceTile:getPosition())
        return true
    end
    return false
end


action:aid(config.lever)
action:register()
 
I just tested it, and it worked perfectly! It now accepts both genders, male and female. The post has been corrected, all good!

Lua:
local config = {
    lever = 18563,
    pricePos = { x = 653, y = 734, z = 7 },
    addonName = { "first", "second" },
    outfits = {
        [50002] = {
            items = {
                { id = 5890, count = 100 }
            },
            outfit = {
                [PLAYERSEX_FEMALE] = 136,
                [PLAYERSEX_MALE] = 128,
            },
            addon = 1
        }
       -- Add more outfits as needed
    }
}

local action = Action()

local removePrice = function(tile, items)
    local tileItems = tile:getItems()
    local itemFound = false

    for _, item in ipairs(items) do
        local count = item.count or 1
        itemFound = false

        for _, tileItem in ipairs(tileItems) do
            if tileItem:getId() == item.id and count > 0 then
                local removeCount = math.min(count, tileItem:getCount())
                count = count - removeCount
                tileItem:remove(removeCount)
                itemFound = true
            end
        end

        if not itemFound then
            print("Item not found:", item.id)
            return false
        end
    end

    return true
end

local checkPrice = function(tile, items)
    local countMap = {}
    for _, item in ipairs(items) do
        local tileCount = countMap[item.id] or tile:getItemCountById(item.id)
        local count = item.count or 1

        if tileCount < count then
            return false
        end

        countMap[item.id] = tileCount - count
    end

    return true
end

function action.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    if item.actionid then
        local priceTile = Tile(config.pricePos)
        if not priceTile then
            print("Error: Unable to create priceTile")
            return false
        end

        if not player:isPremium() then
            player:sendTextMessage(MESSAGE_INFO_DESCR, "Sorry, only premium players may use addon lever.")
            return true
        end

        for sId, o in pairs(config.outfits) do
            local hasOutfit = player:getStorageValue(sId) > 0
            if not hasOutfit and checkPrice(priceTile, o.items) then
                if removePrice(priceTile, o.items) then
                    local lookType = o.outfit[player:getSex()]
                    if lookType then
                        local outfit = Outfit(lookType)
                        outfit.lookAddons = o.addon or 1
                        local addonName = config.addonName[outfit.lookAddons] or ""
                        priceTile:getPosition():sendMagicEffect(CONST_ME_HITBYFIRE)
                        player:setStorageValue(sId, 1)
                        player:addOutfitAddon(lookType, outfit.lookAddons)
                        player:sendTextMessage(MESSAGE_INFO_DESCR, ("You have unlocked the addon %s %s!"):format(addonName, outfit.name))
                        return true
                    end
                else
                    player:say("You don't have the necessary items!", TALKTYPE_ORANGE_1, false, player, priceTile:getPosition())
                    return true
                end
            end
        end

        player:say("You do not have the necessary items or you already have this addon!", TALKTYPE_ORANGE_1, false, player, priceTile:getPosition())
        return true
    end
    return false
end


action:aid(config.lever)
action:register()

Make an addon for MALE. Then change its gender with a command, rune whatever.
Open the outifts window - you will see that you cannot see the previously made addons when you change the gender.

local lookType = o.outfit[player:getSex()] - this line does not choose only male or female outfits?
If so, the addon is only given to a man or woman on a permanent basis, not both.
 
I solved this !!!
Delete: local sex = player:getSex()
Add:
local lookType = o.outfit[0]
local lookTyps = o.outfit[1]

player:addOutfitAddon(lookTyps, outfit.lookAddons)

Complete! All working perfect!
 
Back
Top