12p Posted October 25, 2011 Share Posted October 25, 2011 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
Aibo Posted October 25, 2011 Share Posted October 25, 2011 well it is possible. you're doing something wrong then. Link to comment
BinSlayer1 Posted October 25, 2011 Share Posted October 25, 2011 getElementsByType() always returns a table of elements.. so it is possible to add elements to tables I suggest you use table.insert(table, element) http://pgl.yoyo.org/luai/i/table.insert Link to comment
Castillo Posted October 25, 2011 Share Posted October 25, 2011 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
12p Posted October 25, 2011 Author Share Posted October 25, 2011 (edited) 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 October 25, 2011 by Guest Link to comment
Aibo Posted October 25, 2011 Share Posted October 25, 2011 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
12p Posted October 25, 2011 Author Share Posted October 25, 2011 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
Aibo Posted October 25, 2011 Share Posted October 25, 2011 well then your problem is that table.insert doesn't return anything. try #thetable after insert and see. Link to comment
12p Posted October 25, 2011 Author Share Posted October 25, 2011 Again, I DON'T KNOW WHAT THE HELL I CHANGED BUT NOW IS WORKING. OMG THERE MUST BE SOMETHING WRONG WITH ME Thank you guys for trying to help me. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now