• 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 [TFS1.3Alkarius8.6]Gold_change.lua

n3crozzy

Member
Joined
Apr 23, 2021
Messages
48
Reaction score
14
Hi, I have a problem with the script. The script seems to work but not completely. It doesn't change my gp from bodies and other backpacks. It only changes from the main backpack and from the ground. Could someone explain to me what I'm doing wrong?
Maybe the script I have is written incorrectly?

LUA:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end

    local goldToPlatinum = 100 -- 100 Gold Coins = 1 Platinum Coin
    local platinumToCrystal = 100 -- 100 Platinum Coins = 1 Crystal Coin
    local platinumToGold = 1 -- 1 Platinum Coin = 100 Gold Coins
    local crystalToPlatinum = 1 -- 1 Crystal Coin = 100 Platinum Coins

    local container = player:getSlotItem(CONST_SLOT_BACKPACK) -- Takes the player's backpack

    if not container then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You need a backpack to exchange coins.")
        return true
    end

    -- Function to exchange coins in the backpack
    local function exchangeCoinsInContainer(container, itemId, removeCount, addItemId, addCount, successMessage)
        if not container then return false end
        for i = 0, container:getSize() - 1 do
            local item = container:getItem(i)
            if item and item:getId() == itemId and item:getCount() >= removeCount then
                item:remove(removeCount)
                container:addItem(addItemId, addCount)
                player:sendTextMessage(MESSAGE_INFO_DESCR, successMessage)
                return true
            end
        end
        return false
    end

    -- Function to exchange coins on the ground
    local function exchangeCoinsOnGround(position, itemId, removeCount, addItemId, addCount, successMessage)
        local tile = Tile(position)
        if tile then
            local item = tile:getItemById(itemId)
            if item and item:getCount() >= removeCount then
                item:remove(removeCount)
                player:addItem(addItemId, addCount)
                player:sendTextMessage(MESSAGE_INFO_DESCR, successMessage)
                return true
            end
        end
        return false
    end

    -- First we try to convert the coins from the ground
    if item.itemid == 2148 then -- Gold Coin
        if exchangeCoinsOnGround(fromPosition, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
            return true
        end

    elseif item.itemid == 2152 then -- Platinum Coin
        if exchangeCoinsOnGround(fromPosition, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
            return true
        elseif exchangeCoinsOnGround(fromPosition, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
            return true
        end

    elseif item.itemid == 2160 then -- Crystal Coin
        if exchangeCoinsOnGround(fromPosition, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
            return true
        end
    end

    -- If the exchange from the ground was not possible, we check the backpack
    if item.itemid == 2148 then -- Gold Coin
        if exchangeCoinsInContainer(container, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
            return true
        end

    elseif item.itemid == 2152 then -- Platinum Coin
        if exchangeCoinsInContainer(container, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
            return true
        elseif exchangeCoinsInContainer(container, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
            return true
        end

    elseif item.itemid == 2160 then -- Crystal Coin
        if exchangeCoinsInContainer(container, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
            return true
        end
    end

    return true
end
 
Last edited by a moderator:
LUA:
 local container = player:getSlotItem(CONST_SLOT_BACKPACK) -- Takes the player's backpack

if not container then return false end
Post automatically merged:

LUA:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local player = Player(cid)
    if not player then
        return false
    end

    local goldToPlatinum = 100 -- 100 Gold Coins = 1 Platinum Coin
    local platinumToCrystal = 100 -- 100 Platinum Coins = 1 Crystal Coin
    local platinumToGold = 1 -- 1 Platinum Coin = 100 Gold Coins
    local crystalToPlatinum = 1 -- 1 Crystal Coin = 100 Platinum Coins

    local container = player:getSlotItem(CONST_SLOT_BACKPACK) -- Takes the player's backpack

    if not container then
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You need a backpack to exchange coins.")
        return true
    end

    -- Function to exchange coins in the backpack
    local function exchangeCoinsInContainer(container, itemId, removeCount, addItemId, addCount, successMessage)
        if not container then return false end
        for i = 0, container:getSize() - 1 do
            local item = container:getItem(i)
            if item and item:getId() == itemId and item:getCount() >= removeCount then
                item:remove(removeCount)
                container:addItem(addItemId, addCount)
                player:sendTextMessage(MESSAGE_INFO_DESCR, successMessage)
                return true
            end
        end
        return false
    end

    -- Function to exchange coins on the ground
    local function exchangeCoinsOnGround(position, itemId, removeCount, addItemId, addCount, successMessage)
        local tile = Tile(position)
        if tile then
            local item = tile:getItemById(itemId)
            if item and item:getCount() >= removeCount then
                item:remove(removeCount)
                player:addItem(addItemId, addCount)
                player:sendTextMessage(MESSAGE_INFO_DESCR, successMessage)
                return true
            end
        end
        return false
    end

    -- check non player container
    local itemContainer = item:getParent()
    if itemContainer and itemContainer:isContainer() then
        if item.itemid == 2148 then -- Gold Coin
            if exchangeCoinsInContainer(itemContainer, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
                return true
            end
        elseif item.itemid == 2152 then -- Platinum Coin
            if exchangeCoinsInContainer(itemContainer, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
                return true
            elseif exchangeCoinsInContainer(itemContainer, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
                return true
            end
        elseif item.itemid == 2160 then -- Crystal Coin
            if exchangeCoinsInContainer(itemContainer, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
                return true
            end
        end
    end

    -- First we try to convert the coins from the ground
    if item.itemid == 2148 then -- Gold Coin
        if exchangeCoinsOnGround(fromPosition, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
            return true
        end

    elseif item.itemid == 2152 then -- Platinum Coin
        if exchangeCoinsOnGround(fromPosition, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
            return true
        elseif exchangeCoinsOnGround(fromPosition, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
            return true
        end

    elseif item.itemid == 2160 then -- Crystal Coin
        if exchangeCoinsOnGround(fromPosition, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
            return true
        end
    end

    -- If the exchange from the ground was not possible, we check the backpack
    if item.itemid == 2148 then -- Gold Coin
        if exchangeCoinsInContainer(container, 2148, 100, 2152, 1, "You have exchanged 100 Gold Coins for 1 Platinum Coin.") then
            return true
        end

    elseif item.itemid == 2152 then -- Platinum Coin
        if exchangeCoinsInContainer(container, 2152, 100, 2160, 1, "You have exchanged 100 Platinum Coins for 1 Crystal Coin.") then
            return true
        elseif exchangeCoinsInContainer(container, 2152, 1, 2148, 100, "You have exchanged 1 Platinum Coin for 100 Gold Coins.") then
            return true
        end

    elseif item.itemid == 2160 then -- Crystal Coin
        if exchangeCoinsInContainer(container, 2160, 1, 2152, 100, "You have exchanged 1 Crystal Coin for 100 Platinum Coins.") then
            return true
        end
    end

    return true
end
 

Similar threads

Back
Top