• 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+ Explain and example please

T

Tibia Demon

Guest
Can someone with higher Lua level tell me how to write the 3 examples below on a table or best way to beautifying it

Lua:
if (player:getItemCount(2672) >= 1) and (player:getItemCount(2671) >= 1) and (player:getItemCount(2670) >= 1) and (player:getItemCount(2669) >= 1) and (player:getItemCount(2668) >= 1) and (player:getItemCount(2667) >= 1) and (player:getItemCount(2666) >= 1) then

Lua:
if (player:removeItem(2149, 1)) and (player:removeItem(2150, 1)) and (player:removeItem(2151, 1)) and (player:removeItem(2153, 1)) and (player:removeItem(2154, 1)) and (player:removeItem(2155, 1)) and (player:removeItem(2156, 1)) then

Lua:
if player:getSlotItem(CONST_SLOT_HEAD).itemid == X[i][1] and player:getSlotItem(CONST_SLOT_ARMOR).itemid == X[i][2] and player:getSlotItem(CONST_SLOT_LEGS).itemid == X[i][3] and player:getSlotItem(CONST_SLOT_FEET).itemid == X[i][4] and player:getSlotItem(CONST_SLOT_RING).itemid == X[i][5] and player:getSlotItem(CONST_SLOT_BACKPACK).itemid == X[i][6] and player:getSlotItem(CONST_SLOT_LEFT).itemid == X[i][7] then

I don't have a full script and the 3 examples above are just created randomly now.
 
Solution
Lua:
local config = {
    {id = 2155, count = 1},
    {id = 2154, count = 1},
    {id = 2153, count = 1},
    {id = 2151, count = 1},
    {id = 2150, count = 1},
    {id = 2149, count = 1}
}

local makeHelmet = Action()
function makeHelmet.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local haveAllItems = true
    -- Check if player have the items
    for _, item in ipairs(config) do
        -- If a item is missing, we stop the loop by using break and set haveAllItems to false
        if player:getItemCount(item.id) < item.count then
            haveAllItems = false
            break
        end
    end

    -- Here we check if the haveAllItems is true or false
    if haveAllItems then
        -- We got all the...
Probably not, because you're counting a whole bunch of different things together...

Like you're checking if you have 1 of each 7 different items....
Checking if the player had and 1 of each of 7 items and removed 1.
Checking if all users items are specific items...



Looks messy but thats about it.


Would really depend on the rest of the script and if that can be cleaned up to do all this in a better way, but out of context it's hard to say.
 
I don't have a script.
I face same situation as the 3 I have showed above where I need many checks.
The question here is what would you or other Lua scripters do in this situation? Would you use [and and and and] or how would you create the check?
Lemme just create example scripts now and tell me how would you write it
Lua:
local testscript = Action()

function testscript.onUse(player, item, fromPos, target, toPos, isHotkey)
    if (player:removeItem(2149, 1)) and (player:removeItem(2150, 1)) and (player:removeItem(2151, 1)) and (player:removeItem(2153, 1)) and (player:removeItem(2154, 1)) and (player:removeItem(2155, 1)) and (player:removeItem(2156, 1)) then
    player:addItem(2471, 1)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    else
    player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end
    return true
    end
testscript:id(2149, 2150, 2151, 2153, 2154, 2155, 2156)
testscript:register()
Lua:
local testscripttwo = Action()

local nplayerpos = {x = 1000, y = 1000, z = 7}

function testscripttwo.onUse(player, item, fromPos, target, toPos, isHotkey)
    if (player:getItemCount(2672) >= 1) and (player:getItemCount(2671) >= 1) and (player:getItemCount(2670) >= 1) and (player:getItemCount(2669) >= 1) and (player:getItemCount(2668) >= 1) and (player:getItemCount(2667) >= 1) and (player:getItemCount(2666) >= 1) then    player:addItem(2471, 1)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    player:teleportTo(nplayerpos)
    else
    player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end
    return true
    end
testscripttwo:id(2672, 2671, 2670, 2669, 2668, 2667, 2666, 2471)
testscripttwo:register()
 
You can do this in many ways. Here is one way:
Lua:
local config = {
    {id = 2666, count = 1},
    {id = 2668, count = 1},
    {id = 2669, count = 1},
    {id = 2670, count = 1},
    {id = 2671, count = 1},
    {id = 2672, count = 1}
}

local haveAllItems = true
local removedAllItems = true
local wearingAllItems = true
for _, item in ipairs(config) do
    if player:getItemCount(item.id) < item.count then
        haveAllItems = false
    end

    if not player:removeItem(item.id, item.count) then
        removedAllItems = false
    end

    if player:getSlotItem(CONST_SLOT_HEAD).itemid ~= item.id then
        wearingAllItems = false
    end
end

if not haveAllItems then
    print("Something is missing")
end
 
I am trying to figure out how to do it but I think I am doing it wrong.
This is how I wrote it. I removed the slots check part.
Lua:
local makehelm = Action()

local config = {
    {id = 2155, count = 1},
    {id = 2154, count = 1},
    {id = 2153, count = 1},
    {id = 2151, count = 1},
    {id = 2150, count = 1},
    {id = 2149, count = 1}
}

local haveAllItems = true
local removedAllItems = true

function makehelm.onUse(player, item, fromPos, target, toPos, isHotkey)
    for _, item in ipairs(config) do
    if player:getItemCount(item.id) < item.count then
        haveAllItems = false
    end
    if not player:removeItem(item.id, item.count) then
        removedAllItems = false
    end
    player:addItem(2471, 1)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    if not removedAllItems then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You dont have the items.")
    player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end
    return true
    end
    end
makehelm:id(2155, 2154, 2153, 2151, 2150, 2149)
makehelm:register()
It should check for all items and remove it all if exist to create another item [helmet] that is what it used to do when I had [and-and-and] but it doesn't do same here.
table1.gif
table2.gif
 
I am trying to figure out how to do it but I think I am doing it wrong.
This is how I wrote it. I removed the slots check part.
Lua:
local makehelm = Action()

local config = {
    {id = 2155, count = 1},
    {id = 2154, count = 1},
    {id = 2153, count = 1},
    {id = 2151, count = 1},
    {id = 2150, count = 1},
    {id = 2149, count = 1}
}

local haveAllItems = true
local removedAllItems = true

function makehelm.onUse(player, item, fromPos, target, toPos, isHotkey)
    for _, item in ipairs(config) do
    if player:getItemCount(item.id) < item.count then
        haveAllItems = false
    end
    if not player:removeItem(item.id, item.count) then
        removedAllItems = false
    end
    player:addItem(2471, 1)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    if not removedAllItems then
    player:sendTextMessage(MESSAGE_INFO_DESCR, "You dont have the items.")
    player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end
    return true
    end
    end
makehelm:id(2155, 2154, 2153, 2151, 2150, 2149)
makehelm:register()
It should check for all items and remove it all if exist to create another item [helmet] that is what it used to do when I had [and-and-and] but it doesn't do same here.
View attachment 59430
View attachment 59431
It happens because you are checking if you removed all items and not if you have all items.
It should be done like this:
1. for loop - check if u have each item
2. check if you have all items (if not then return true)
3. another for loop - remove all items
4. add helmet
 
I tried to make it like this
Lua:
local makehelm = Action()

local config = {
    {id = 2155, count = 1},
    {id = 2154, count = 1},
    {id = 2153, count = 1},
    {id = 2151, count = 1},
    {id = 2150, count = 1},
    {id = 2149, count = 1}
}

local haveAllItems = true
local removedAllItems = true

function makehelm.onUse(player, item, fromPos, target, toPos, isHotkey)
    for _, item in ipairs(config) do
    if player:getItemCount(item.id) < item.count then
        haveAllItems = false
    end
    if not haveAllItems then
       player:sendTextMessage(MESSAGE_INFO_DESCR, "You dont have the items.")
       player:getPosition():sendMagicEffect(CONST_ME_POFF)
       return true
       end
    if not player:removeItem(item.id, item.count) then
        removedAllItems = false
    end
    player:addItem(2471, 1)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
    end
makehelm:id(2155, 2154, 2153, 2151, 2150, 2149)
makehelm:register()
I found 2 different problems.
1st. It removed all items and adds a helmet instead of each item so (6 helmets) not only 1.
2nd. When I don't have the items at first test it sets local haveAllItems to false and stays false no matter what I do next. It never changes to true.

Can you explain with an example? I find it hard to understand without examples but I keep trying.
 
Lua:
local config = {
    {id = 2155, count = 1},
    {id = 2154, count = 1},
    {id = 2153, count = 1},
    {id = 2151, count = 1},
    {id = 2150, count = 1},
    {id = 2149, count = 1}
}

local makeHelmet = Action()
function makeHelmet.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local haveAllItems = true
    -- Check if player have the items
    for _, item in ipairs(config) do
        -- If a item is missing, we stop the loop by using break and set haveAllItems to false
        if player:getItemCount(item.id) < item.count then
            haveAllItems = false
            break
        end
    end

    -- Here we check if the haveAllItems is true or false
    if haveAllItems then
        -- We got all the items, time to remove them. We dont need to make another if statement since getItemCount have already checked the items exsist
        for _, item in ipairs(config) do
            player:removeItem(item.id, item.count)
        end

        player:addItem(2471, 1)
        player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    else
        player:sendTextMessage(MESSAGE_INFO_DESCR, "You dont have the items.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end
    return true
end

-- Since you use the same id as the config, we can use a loop to add them
for _, item in ipairs(config) do
    makeHelmet:id(item.id)
end
makeHelmet:register()
 
Solution
Back
Top