Jump to content

How to insert some values into a table?


12p

Recommended Posts

Well, I wanna make a Undo system.

I'll explain how it works:

I create a global elements table.

For every player, I create a sub-table.

When a player creates an element, that element is being inserted into the player's table.

Then, when the player triggers the Undo action, the element is removed from the player's table and destroyed.

Easy, isn't it?

Well, the problem is that I cannot insert elements into tables. I thought it was possible.

I don't have any other idea to do it. How to?

Link to comment
local myTable = {} 
  
addCommandHandler("insert", 
function (player) 
     if not myTable[player] then myTable[player] = {} end 
     table.insert(myTable[player], createObject(980, 0, 0, 0)) 
end) 
  
addCommandHandler("get", 
function (player) 
    for index, ob in pairs(myTable[player]) do 
        local x, y, z = getElementPosition(ob) 
        outputChatBox(tostring(x) ..", ".. tostring(y) ..", ".. tostring(z)) 
    end 
end) 

That should work.

Link to comment

Okay. Here is the code I use.

GLOBAL_ELEMENTS_TABLE = { } 
function getPlayerElementsTable ( p ) return GLOBAL_ELEMENTS_TABLE[p] end 
  
addEventHandler ( "onPlayerCreateElement", getRootElement ( ), 
function ( ele, id, x, y, z, r, keep, weapon ) 
    local thetable = getPlayerElementsTable ( source ) 
    outputChatBox ( type ( thetable ) ) 
    if ele == "vehicle" then 
        local v = createVehicle ( id, x, y, z ) 
        setVehicleRotation ( v, 0, 0, r ) 
        if table.insert ( thetable, v ) then outputChatBox ( "Element Vehicle was added to "..getPlayerName ( source ) .. "'s table" ) end 
    elseif ele == "object" then 
        local obj = createObject ( id, x, y, z ) 
        setElementRotation ( obj, 0, 0, r ) 
        table.insert ( thetable, obj  ) 
    elseif ele == "ped" then 
        local ped = createPed ( id, x, y, z ) 
        setElementRotation ( ped, 0, 0, r ) 
        if getWeaponNameFromID ( weapon ) then giveWeapon ( ped, weapon, 999999, true ) end 
        table.insert ( thetable, ped ) 
    end 
end ) 

Everything works fine; the element is created, the table is created, but someway it doesn't insert the element into the table.

Edited by Guest
Link to comment

are you sure vehicle is created?

you never define GLOBAL_ELEMENTS_TABLE[p] as a table, so it cant insert anything there obviously:

function getPlayerElementsTable ( p )  
  if type(GLOBAL_ELEMENTS_TABLE[p]) ~= "table" then GLOBAL_ELEMENTS_TABLE[p] = {} end 
  return GLOBAL_ELEMENTS_TABLE[p]  
end 
  

Link to comment

If the vehicle is not created, I speak asgtyhythyhdf's language.

So, yep. I see the vehicle created and some of my friends who test the script, also see it.

EDIT: @Aibo, here is other part of my code:

function resetConfig ( p ) 
    dettachElementFromPlayer ( p ) 
    GLOBAL_ELEMENTS_TABLE[p] = { } 
    --More things but doesn't matter 
end 
  
addEventHandler ( "onPlayerJoin", getRootElement ( ), function ( ) resetConfig ( source ) end ) 
for i, p in ipairs ( getElementsByType ( "player" ) ) do resetConfig ( p ) end 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...