• 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+ my Table keeps expanding

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
Hi, I wanted to add multiple positions and a number that will change (0/1) for specific player to the table and after few seconds remove it. Code so far:

1. global.lua
Lua:
GLOBAL_MYINFO = {}

2. spells\mySpell.lua
Lua:
local createdItemsPos =
{
       Position(targetPos.x - 1, targetPos.y, targetPos.z), -- left
       Position(targetPos.x + 1, targetPos.y, targetPos.z), -- right
       Position(targetPos.x, targetPos.y + 1, targetPos.z), -- top
       Position(targetPos.x, targetPos.y - 1, targetPos.z), -- bottom
}

local function myFunction(cid)
local player = Player(cid)
local target = player:getTarget()

    for tableId, tableTrigger in pairs(GLOBAL_MYINFO) do
       if tableId == target:getId() then
           if tableTrigger[1] == 1 then
               print("triggered")
               GLOBAL_MYINFO[target:getId()] = nil -- this should clear tables
           end
       end
   end
end

function onCastSpell(cid, var)
    local player = Player(cid)
   local target = player:getTarget()
local targetPos = target:getPosition()

GLOBAL_MYINFO[target:getId()] = {0, createdItemsPos}

   addEvent(myFunction, 8000, cid)

return true
end

3. actions\spellClick.lua
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
local creature = Creature(cid)
 
   print("\nNEW LINE---")
   for tableId, tableTrigger in pairs(GLOBAL_MYINFO) do
       if creature:getId() ~= tableId then -- if creature ID is not in the table then stop execution
           return false
       end
     
       for k, v in pairs(tableTrigger[2]) do
           print(k, v)
         
       end

       tableTrigger[1] = 1 -- trigger 
   end

   return true
end

This is what happens when you execute spell first time and then click on item:

L1PFKVb.png


This is what happens when you execute spell second time and then click on item: (as you can see, the table increased)

IyOZyFj.png


And this is how the table is structured I guess:

GLOBAL_INFO = {
21732837 = {0, {{X, Y, Z}, {X, Y, Z}, {X, Y, Z}}}
}

/Edit

Not even this helps in myFunction:
GLOBAL_TRIGGERED = {}
 
Last edited:
Solution
Move
Lua:
local createdItemsPos = {}
inside of
Code:
function onCastSpell(cid, var)

Currently you are adding all positions to createdItemsPos everytime the spell is cast.
Then pass the local createdItemsPos table to myFunction to access it there e.g. addEvent(myFunction, 8000, myArray, createdItemsPos).
i don't see where you're adding to the tableTrigger at all, all i see is the assignment of GLOBAL_INFO[target:getId()] = {0, createdItemsPos}
where createdItemsPos is tableTrigger, i can't see any code where you're adding indexes to that table where it would make it increase by 12 each time you run your spell
are you sure this is the full script(s)?
 
i don't see where you're adding to the tableTrigger at all, all i see is the assignment of GLOBAL_INFO[target:getId()] = {0, createdItemsPos}
where createdItemsPos is tableTrigger, i can't see any code where you're adding indexes to that table where it would make it increase by 12 each time you run your spell
are you sure this is the full script(s)?

No its not the full script. I want to simplify it as much as possible. Everything is seen above

GLOBAL_MYINFO[target:getId()] = {0, createdItemsPos} - Where 0, it is the actual trigger, it will change to 1 once the player clicks on item
 
how am i supposed to help if you don't post all of it
the problem is you're adding 12 indexes to createdItemsPos inside of that table every single time your script is run, post that part at least
or if you want it to be completely removed after those 8 seconds in your addevent set tableTrigger[2] to {} (an empty table)
you should also check if player exists in your callback function for your addEvent before you use any methods on it, since the player may not exist on the server after 8 seconds, you could crash the server by attempting to use an object that doesn't exist
 
No its not the full script. I want to simplify it as much as possible. Everything is seen above

You should post the whole script, people can't help you resolve issues if you only give sections, if you knew exactly what was wrong with the script then you wouldn't be asking for help with it.

I am not being an ass I am just being realistic.
 
how am i supposed to help if you don't post all of it
the problem is you're adding 12 indexes to createdItemsPos inside of that table every single time your script is run, post that part at least
or if you want it to be completely removed after those 8 seconds in your addevent set tableTrigger[2] to {} (an empty table)
you should also check if player exists in your callback function for your addEvent before you use any methods on it, since the player may not exist on the server after 8 seconds, you could crash the server by attempting to use an object that doesn't exist
You should post the whole script, people can't help you resolve issues if you only give sections, if you knew exactly what was wrong with the script then you wouldn't be asking for help with it.

I am not being an ass I am just being realistic.

here is the script but as I said its only more difficult to read

spells\mySpell.lua
Lua:
local combat = Combat()
combat:setParameter(COMBAT_PARAM_AGGRESSIVE, false)

local itemId = 2789
local createdItemsPos = {}

local function removeItems()
    for _, createdItem in ipairs(createdItemsPos) do
        local item = Tile(createdItem):getItemById(itemId)
        if item then
            item:remove()
        end
    end
end

local function myFunction(myArray)
    for tableId, tableTrigger in pairs(GLOBAL_MYINFO) do
        if tableId == myArray.target then
            if tableTrigger[1] == 1 then
                print("triggered")
                GLOBAL_MYINFO[myArray.target] = nil
            end
        end
    end
 
    return true
end

function onCastSpell(cid, var)
    local player = Player(cid)
    local target = player:getTarget()
    local targetPos = target:getPosition()
 
    local area = {
        Position(targetPos.x - 1, targetPos.y, targetPos.z), -- left
        Position(targetPos.x + 1, targetPos.y, targetPos.z), -- right
        Position(targetPos.x, targetPos.y + 1, targetPos.z), -- top
        Position(targetPos.x, targetPos.y - 1, targetPos.z), -- bottom
   
        Position(targetPos.x - 2, targetPos.y - 1, targetPos.z),
        Position(targetPos.x + 2, targetPos.y - 1, targetPos.z),
        Position(targetPos.x - 2, targetPos.y + 1, targetPos.z),
        Position(targetPos.x + 2, targetPos.y + 1, targetPos.z),
   
        Position(targetPos.x - 1, targetPos.y - 2, targetPos.z),
        Position(targetPos.x + 1, targetPos.y - 2, targetPos.z),
        Position(targetPos.x - 1, targetPos.y + 2, targetPos.z),
        Position(targetPos.x + 1, targetPos.y + 2, targetPos.z)
    }

    for _, pos in ipairs(area) do
        local item = Tile(pos):getItemById(itemId)
        if not item then
            createdItemsPos[#createdItemsPos + 1] = pos
            Game.createItem(itemId, 1, pos)
        end
    end
 
    local myArray = {
        originalPlayerId = player:getId(),
        target = target:getId(),
        targetPos = targetPos
    }

    GLOBAL_MYINFO[target:getId()] = {0, createdItemsPos}
 
    addEvent(myFunction, 8000, myArray)
 
    return combat:execute(cid, var)
end


actions\spellClick.lua
Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local creature = Creature(cid)
 
    print("\nNEW LINE---")
    for tableId, tableTrigger in pairs(GLOBAL_MYINFO) do
        if creature:getId() ~= tableId then  -- if creature ID is not in the table then stop execution
            return false
        end
   
        print("Creature ".. creature:getId() .. " Triggered: " .. tableTrigger[1] .. " PositionX: " .. tableTrigger[2][1].x .. " PositionY: " .. tableTrigger[2][1].y .. " PositionZ: " .. tableTrigger[2][1].z)
   
        for k, v in pairs(tableTrigger[2]) do
            print(k, v)
        end

        tableTrigger[1] = 1 -- trigger
    end
    return true
end

also the tableTrigger name is just value for key in table. Just my naming conventions

usually it would be

for k,v in pairs(GLOBAL_MYINFO) do

// edit 2

Apparently the main table is empty after executing this
GLOBAL_INFO = {}

But the tables inside are still there :confused:
 
Last edited:
Move
Lua:
local createdItemsPos = {}
inside of
Code:
function onCastSpell(cid, var)

Currently you are adding all positions to createdItemsPos everytime the spell is cast.
Then pass the local createdItemsPos table to myFunction to access it there e.g. addEvent(myFunction, 8000, myArray, createdItemsPos).
 
Solution
Move
Lua:
local createdItemsPos = {}
inside of
Code:
function onCastSpell(cid, var)

Currently you are adding all positions to createdItemsPos everytime the spell is cast.
Then pass the local createdItemsPos table to myFunction to access it there e.g. addEvent(myFunction, 8000, myArray, createdItemsPos).

OMG THANK YOU SO MUCH. I was so confused and its just the small details. I guess I was focusing more on the main function with the GLOBAL variable that I have forgotten about the entire script functionality. And yes you wouldn't solve this if I wouldn't post the complete script my bad
 
Back
Top