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

Receive tibia coins after use some items (tfs 1.3)

RatoKing

New Member
Joined
Mar 17, 2021
Messages
11
Solutions
1
Reaction score
2
GitHub
RatoKing
Hello guys,

I created a script when you use tokens (gold, platinum, etc..) you have certain amount o tibia coin but what I did isn't the right way.
Could someone help me to get it better?

Lua:
local setting = {
    [25376] = {Type = "iron", count = 1000},
    [25377] = {Type = "gold", count = 1500},
    [25378] = {Type = "cooper", count = 2000},
    [25379] = {Type = "platinum", count = 2500},
    [25380] = {Type = "titanium", count = 3000}
}


local tibiaCoin = Action()


function tibiaCoin.onUse(player, item, fromPosition, target, toPosition, isHotkey)
        local foundItem = setting[item.itemid]
            if not(foundItem) then
            return
        end
        if foundItem.Type == "iron" then
            db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
            player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
        elseif foundItem.Type == "gold" then
            db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
            player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
        elseif foundItem.Type == "cooper" then
            db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
            player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
        elseif foundItem.Type == "platinum" then
            db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
            player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
        elseif foundItem.Type == "titanium" then
            db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
            player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
            player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
        end 
        item:remove(1)
              
        return true
end




tibiaCoin:id(25376, 25377, 25378, 25379, 25380)
tibiaCoin:register()
 
Solution
You don't need to check type because you know the itemid and you specifically select which table to use. Might aswell remove Type from those tables
Code:
-- if item.itemid was 25376
-- then you would have
foundItem = {Type = "iron", count = 1000}

Try:
Lua:
local setting = {
    [25376] = {count = 1000},
    [25377] = {count = 1500},
    [25378] = {count = 2000},
    [25379] = {count = 2500},
    [25380] = {count = 3000}
}

local tibiaCoin = Action()
function tibiaCoin.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local foundItem = setting[item.itemid]
    if not foundItem then
        return true
    end

    db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id="...

Snavy

Bakasta
Senator
Joined
Apr 1, 2012
Messages
1,244
Solutions
71
Reaction score
546
Location
Hell
You don't need to check type because you know the itemid and you specifically select which table to use. Might aswell remove Type from those tables
Code:
-- if item.itemid was 25376
-- then you would have
foundItem = {Type = "iron", count = 1000}

Try:
Lua:
local setting = {
    [25376] = {count = 1000},
    [25377] = {count = 1500},
    [25378] = {count = 2000},
    [25379] = {count = 2500},
    [25380] = {count = 3000}
}

local tibiaCoin = Action()
function tibiaCoin.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local foundItem = setting[item.itemid]
    if not foundItem then
        return true
    end

    db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
    player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
    item:remove(1)
    return true
end

tibiaCoin:id(25376, 25377, 25378, 25379, 25380)
tibiaCoin:register()
 
Solution

Evil Puncker

prolonged absenteeism
TFS Developer
Joined
May 30, 2009
Messages
7,900
Solutions
197
Reaction score
3,707
also you can use:
Lua:
for k, v in pairs(setting) do
    tibiaCoin:id(k)
end

instead of:
Lua:
tibiaCoin:id(25376, 25377, 25378, 25379, 25380)


and (if you are using otservbr):
Lua:
player:addTibiaCoins(foundItem.count)

instead of:
Lua:
db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
 
OP
R

RatoKing

New Member
Joined
Mar 17, 2021
Messages
11
Solutions
1
Reaction score
2
GitHub
RatoKing
You don't need to check type because you know the itemid and you specifically select which table to use. Might aswell remove Type from those tables
Code:
-- if item.itemid was 25376
-- then you would have
foundItem = {Type = "iron", count = 1000}

Try:
Lua:
local setting = {
    [25376] = {count = 1000},
    [25377] = {count = 1500},
    [25378] = {count = 2000},
    [25379] = {count = 2500},
    [25380] = {count = 3000}
}

local tibiaCoin = Action()
function tibiaCoin.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local foundItem = setting[item.itemid]
    if not foundItem then
        return true
    end

    db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())
    player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
    player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
    item:remove(1)
    return true
end

tibiaCoin:id(25376, 25377, 25378, 25379, 25380)
tibiaCoin:register()

Yeah, it worked well. Thanks.
Post automatically merged:

also you can use:
Lua:
for k, v in pairs(setting) do
    tibiaCoin:id(k)
end

instead of:
Lua:
tibiaCoin:id(25376, 25377, 25378, 25379, 25380)


and (if you are using otservbr):
Lua:
player:addTibiaCoins(foundItem.count)

instead of:
Lua:
db.query("UPDATE `accounts` SET `coins` = `coins` + " ..foundItem.count.. " WHERE id=" ..player:getAccountId())

Thanks for de contribuiton.
Post automatically merged:

If someone ever need it:

Lua:
local setting = {
    [25376] = {count = 1000},
    [25377] = {count = 1500},
    [25378] = {count = 2000},
    [25379] = {count = 2500},
    [25380] = {count = 3000}
}


local tibiaCoin = Action()


function tibiaCoin.onUse(player, item, fromPosition, target, toPosition, isHotkey)
        local foundItem = setting[item.itemid]
            if not(foundItem) then
            return
        end
        player:addTibiaCoins(foundItem.count)
        player:getPosition():sendMagicEffect(CONST_ME_MAGIC_RED)
        player:sendTextMessage(MESSAGE_EVENT_ADVANCE, 'You have received '..foundItem.count..' tibia coins.')
        item:remove(1)
        return true
end
for k, v in pairs(setting) do
    tibiaCoin:id(k)
end
tibiaCoin:register()
 
Last edited:
Top