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

TFS 1.X+ [Lua]AutoGold Exchange

LightTenshimaru

LightTenshimaru
Joined
Mar 15, 2014
Messages
31
Best answers
0
Reaction score
1
Hello, i've got this code to change Gold Coin into Platinum Coin automatically, but there's no console error and the gold it's not changing.

Lua:
function onThink(creature, interval)
    local player = creature:getPlayer()
    if not player then
        return false
    end
    if player:getItemCount(2148) == 100 then
       player:removeItem(2148, 100)
       player:addItem(2152, 1)  
       player:sendCancelMessage('Converted 100 Gold Coins to 1 Platinum Coin.')
       return true
    end
end
Whats wrong?
 
Last edited:

Infernum

Support Team
Joined
Feb 14, 2015
Messages
5,497
Best answers
547
Reaction score
3,395
did you register it in creaturescripts.xml and login.lua?
another problem i see is you're directly comparing the item count to ONLY 100
decided to write something of my own that's configurable with new ids
Lua:
local exchange = {
    -- [id to exchange] = id to give
    [ITEM_GOLD_COIN] = ITEM_PLATINUM_COIN,
    [ITEM_PLATINUM_COIN] = ITEM_CRYSTAL_COIN
}

function onThink(creature, interval)
    local player = creature:getPlayer()
    if not player then
        return false
    end
    for checkid, exchangeid in pairs(exchange) do
        local count = 0
        while player:getItemCount(checkid) >= 100 do
            local coins = player:getItemById(exchangeid) -- because stacking is weird
            if coins and coins:getCount() < 100 then
                coins:transform(coins:getId(), coins:getCount() + 1)
            else
                player:addItem(exchangeid, 1)
            end
            player:removeItem(checkid, 100)
            count = count + 100
        end
        if count > 0 then
            local checkname = ItemType(checkid):getPluralName()
            local exchangename = (count / 100) == 1 and ItemType(exchangeid):getName() or ItemType(exchangeid):getPluralName()
            player:sendCancelMessage(("Converted %d %s to %d %s"):format(count, checkname, count / 100, exchangename))
        end
    end
    return true
end
edit: fixed the plural name for exchanged items
 
OP
LightTenshimaru

LightTenshimaru

LightTenshimaru
Joined
Mar 15, 2014
Messages
31
Best answers
0
Reaction score
1
did you register it in creaturescripts.xml and login.lua?
another problem i see is you're directly comparing the item count to ONLY 100
decided to write something of my own that's configurable with new ids
Lua:
local exchange = {
    -- [id to exchange] = id to give
    [ITEM_GOLD_COIN] = ITEM_PLATINUM_COIN,
    [ITEM_PLATINUM_COIN] = ITEM_CRYSTAL_COIN
}

function onThink(creature, interval)
    local player = creature:getPlayer()
    if not player then
        return false
    end
    for checkid, exchangeid in pairs(exchange) do
        local count = 0
        while player:getItemCount(checkid) >= 100 do
            local coins = player:getItemById(exchangeid) -- because stacking is weird
            if coins and coins:getCount() < 100 then
                coins:transform(coins:getId(), coins:getCount() + 1)
            else
                player:addItem(exchangeid, 1)
            end
            player:removeItem(checkid, 100)
            count = count + 100
        end
        if count > 0 then
            local checkname = ItemType(checkid):getPluralName()
            local exchangename = (count / 100) == 1 and ItemType(exchangeid):getName() or ItemType(exchangeid):getPluralName()
            player:sendCancelMessage(("Converted %d %s to %d %s"):format(count, checkname, count / 100, exchangename))
        end
    end
    return true
end
edit: fixed the plural name for exchanged items

Works Like a CHARM!
Thank you! <3
 

Infernum

Support Team
Joined
Feb 14, 2015
Messages
5,497
Best answers
547
Reaction score
3,395
here's a better/faster script using actual math instead of a while loop (idk why i wrote it with a while loop in the first place)
Lua:
local exchange = {
    [ITEM_GOLD_COIN] = ITEM_PLATINUM_COIN,
    [ITEM_PLATINUM_COIN] = ITEM_CRYSTAL_COIN
}

function onThink(creature, interval)
    local player = creature:getPlayer()
    if not player then
        return false
    end
    for checkid, exchangeid in pairs(exchange) do
        local count = player:getItemCount(checkid)
        if count >= 100 then
            count = count - (count % 100)
            local exchangecount = count / 100
            local coins = player:getItemById(exchangeid) -- because stacking is weird
            if coins and coins:getCount() < 100 then
                coins:transform(coins:getId(), coins:getCount() + exchangecount)
            else
                player:addItem(exchangeid, exchangecount)
            end
            player:removeItem(checkid, count)
            local checkname = ItemType(checkid):getPluralName()
            local exchangename = (count / 100) == 1 and ItemType(exchangeid):getName() or ItemType(exchangeid):getPluralName()
            player:sendCancelMessage(("Converted %d %s to %d %s"):format(count, checkname, count / 100, exchangename))
        end
    end
    return true
end
 
OP
LightTenshimaru

LightTenshimaru

LightTenshimaru
Joined
Mar 15, 2014
Messages
31
Best answers
0
Reaction score
1
here's a better/faster script using actual math instead of a while loop (idk why i wrote it with a while loop in the first place)
Lua:
local exchange = {
    [ITEM_GOLD_COIN] = ITEM_PLATINUM_COIN,
    [ITEM_PLATINUM_COIN] = ITEM_CRYSTAL_COIN
}

function onThink(creature, interval)
    local player = creature:getPlayer()
    if not player then
        return false
    end
    for checkid, exchangeid in pairs(exchange) do
        local count = player:getItemCount(checkid)
        if count >= 100 then
            count = count - (count % 100)
            local exchangecount = count / 100
            local coins = player:getItemById(exchangeid) -- because stacking is weird
            if coins and coins:getCount() < 100 then
                coins:transform(coins:getId(), coins:getCount() + exchangecount)
            else
                player:addItem(exchangeid, exchangecount)
            end
            player:removeItem(checkid, count)
            local checkname = ItemType(checkid):getPluralName()
            local exchangename = (count / 100) == 1 and ItemType(exchangeid):getName() or ItemType(exchangeid):getPluralName()
            player:sendCancelMessage(("Converted %d %s to %d %s"):format(count, checkname, count / 100, exchangename))
        end
    end
    return true
end
Thanks again.
 
Top