• 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 Potion Lever TFS 1.X - slot problem.

OTcreator

Active Member
Joined
Feb 14, 2022
Messages
425
Solutions
1
Reaction score
44
Hi!
What to change to buy potions for more backpacks and not just the main one, while keeping it so they don't fall to the ground when there's no room in all the slots?

Lua:
local potions = {
    [5001] = {id = 7618, charges = 100, value = 4500}, -- HP
    [5002] = {id = 7588, charges = 100, value = 10000}, -- SHP
    [5003] = {id = 7591, charges = 100, value = 19000}, -- GHP
    [5004] = {id = 7620, charges = 100, value = 5000}, -- MP
    [5005] = {id = 7589, charges = 100, value = 8000}, -- SMP
    [5006] = {id = 7590, charges = 100, value = 12000}, -- GMP
    [5007] = {id = 8472, charges = 100, value = 19000}, -- SP
    [5008] = {id = 8473, charges = 100, value = 31000}, -- UHP
    [5009] = {id = 2273, charges = 100, value = 5000}, -- UH
    [5010] = {id = 2268, charges = 100, value = 10800} -- SD
}

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local potion = potions[item.uid]
    if not potion then
        return false
    end

    local backpack = player:getSlotItem(CONST_SLOT_BACKPACK)
    if not backpack or backpack:getEmptySlots(false) < 1 then
        player:sendCancelMessage("Your main backpack is full. Transfer your belongings to another container.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
        return true
    end   

    local potionId = ItemType(potion.id)
    local itemWeight = potionId:getWeight() * potion.charges
    if player:getFreeCapacity() >= itemWeight then
        if not player:removeTotalMoney(potion.value) then
            player:sendCancelMessage("You don't have ".. potion.value .." gold coins to buy ".. potion.charges .."x ".. potionId:getName() ..".")
            player:getPosition():sendMagicEffect(CONST_ME_POFF)
        else
            player:sendCancelMessage("You are bought ".. potion.charges .."x ".. potionId:getName() .." for ".. potion.value .." gold coins.")
            player:getPosition():sendMagicEffect(CONST_ME_HITAREA)
            player:addItem(potion.id, potion.charges)
        end
        
    else
        player:sendCancelMessage("Sorry, you don't have capacity.")
        player:getPosition():sendMagicEffect(CONST_ME_POFF)
    end

    item:transform(item.itemid == 1945 and 1946 or 1945)
    return true
end
 
container:getEmptySlots([recursive = false])

Recursive means it will check / not check for deeper slots.

So if you change
Lua:
if not backpack or backpack:getEmptySlots(false) < 1 then
to
Lua:
if not backpack or backpack:getEmptySlots(true) < 1 then

It should find all available empty slots, not just one's in the main backpack.

However, if players have unlimited capacity, and can hold containers and containers of items.. it is feasible that they could abuse the recursive feature and lag / crash your server.
 
container:getEmptySlots([recursive = false])

Recursive means it will check / not check for deeper slots.

So if you change
Lua:
if not backpack or backpack:getEmptySlots(false) < 1 then
to
Lua:
if not backpack or backpack:getEmptySlots(true) < 1 then

It should find all available empty slots, not just one's in the main backpack.

However, if players have unlimited capacity, and can hold containers and containers of items.. it is feasible that they could abuse the recursive feature and lag / crash your server.
Thanks you! And is it possible, for example, to easily change the fact that if it has more seats than 5 because then the information about the full slot ?
 
Etc.
If player have max 5 containers full - message and not possibile to buy more.
Well each layer deep you go, it get's wild pretty quick.

layer 1 - 1 backpack - 1 item.
layer 2 - backpack of backpacks - 21 items
layer 3 - bp of bp of bp - 421 items
layer 4 - bp of bp of bp of bp - 8421 items
layer 5 - bp of bp of bp of bp of bp - 168421 items

--
But if this is what you actually want to do, you'd have to write a custom recursive function to go through the containers, layer by layer.
 
Well each layer deep you go, it get's wild pretty quick.

layer 1 - 1 backpack - 1 item.
layer 2 - backpack of backpacks - 21 items
layer 3 - bp of bp of bp - 421 items
layer 4 - bp of bp of bp of bp - 8421 items
layer 5 - bp of bp of bp of bp of bp - 168421 items

--
But if this is what you actually want to do, you'd have to write a custom recursive function to go through the containers, layer by layer.
And yet it's not that simple.
Tell me, how many slots would it have to have to make a crash?
I have a low rate RPG there max would be 500 level :)

Maybe he would first give a function that checks free CAP (because here it will cut off the ability to review many slots in the first place?
 
Back
Top