StefanAlmighty Posted October 3, 2015 Share Posted October 3, 2015 Okay so I've been working on a teleport GUI and it works fine, but I'm at the stage of adding teleports to a table whilst in-game. For some reason, it's not working. My table: local data = { -- X Y Z Int Dim Rot [1] = {"1", "Los Santos", "Pershing Square", 1479.9873046875, -1710.9453125, 13.36874961853, 0, 0, 0}, [2] = {"2", "San Fierro", "Doherty", -1988.5693359375, 507.0029296875, 35.171875, 0, 0, 90}, [3] = {"3", "Las Venturas", "Las Venturas Airport", 1691.6801757813, 1449.1293945313, 10.765375, 0, 0, 268}, } The function I made which adds a teleport to this table: addEvent ("addTeleport",true) addEventHandler ("addTeleport",root, function (tpname, tpdesc) local x, y, z = getElementPosition(source) local rx, ry, rz = getElementRotation(source) -- fetch all the necessary info for the teleport local int = getElementInterior(source) local dim = getElementDimension(source) local nextindex = findNextTeleportID() local nextindexs = tostring(nextindex) table.insert(data, [nextindex] = {nextindexs, tpname, tpdesc, x, y, z, int, dim, rz}) end) The function which finds the next available ID in the table: function findNextTeleportID() for index, val in ipairs (data) do local finalnum = 0 finalnum + 1 end return finalnum + 1 end Any help is appreciated, thanks. Link to comment
pa3ck Posted October 3, 2015 Share Posted October 3, 2015 function findNextTeleportID() for index, val in ipairs (data) do local finalnum = 0 -- creates a finalnum variable each time the loop runs and sets it to 0 finalnum + 1 end return finalnum + 1 end This part is wrong and its not even needed. You can just get the length ( or the size if you like ) the table and add +1, like: local nextIndex = #data + 1 Link to comment
KariiiM Posted October 4, 2015 Share Posted October 4, 2015 Also the loop isn't used so no need this part function findNextTeleportID() for index, val in ipairs (data) do local finalnum = 0 -- creates a finalnum variable each time the loop runs and sets it to 0 finalnum + 1 end return finalnum + 1 end Link to comment
StefanAlmighty Posted October 4, 2015 Author Share Posted October 4, 2015 That didn't seem to fix it, any other issues with my code? Link to comment
TAPL Posted October 4, 2015 Share Posted October 4, 2015 addEvent("addTeleport",true) addEventHandler("addTeleport", root, function(tpname, tpdesc) local x, y, z = getElementPosition(source) local rx, ry, rz = getElementRotation(source) -- fetch all the necessary info for the teleport local int = getElementInterior(source) local dim = getElementDimension(source) table.insert(data, {tostring(#data+1), tpname, tpdesc, x, y, z, int, dim, rz}) end) findNextTeleportID not needed. Link to comment
StefanAlmighty Posted October 4, 2015 Author Share Posted October 4, 2015 Alright, that fixed it. When I restart the resource, any teleports added will be removed so how do I save teleports via XML? 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