Drakath Posted October 18, 2014 Share Posted October 18, 2014 I have a problem with table indexes. I used: #myTable to check how much players are in the table, however it always outputted 0 even though I did put some players in the table. table.maxn(myTable) showed a number higher than 0, however when I did table.random(myTable), it outputted: bad argument #1 to 'random' (interval is empty) This is how I put players into table (Server-side): local myTable= {} for index,player in ipairs(getElementsByType("player")) do if getDistanceBetweenPoints3D(Px,Py,Pz,x,y,z) < 100 then myTable[index] = player outputChatBox("Success") end end It does output success but #myTable is 0. Can someone explain why? Link to comment
ZL|LuCaS Posted October 18, 2014 Share Posted October 18, 2014 I have a problem with table indexes.I used: #myTable to check how much players are in the table, however it always outputted 0 even though I did put some players in the table. table.maxn(myTable) showed a number higher than 0, however when I did table.random(myTable), it outputted: bad argument #1 to 'random' (interval is empty) This is how I put players into table (Server-side): local myTable= {} for index,player in ipairs(getElementsByType("player")) do if getDistanceBetweenPoints3D(Px,Py,Pz,x,y,z) < 100 then myTable[index] = player outputChatBox("Success") end end It does output success but #myTable is 0. Can someone explain why? you need to check line 3 --getDistanceBetweenPoints3D local myTable= {} for index,player in ipairs(getElementsByType("player")) do -- if getDistanceBetweenPoints3D(Px,Py,Pz,x,y,z) < 100 then myTable[index] = player outputChatBox(#myTable) --end end returns players ! Link to comment
Drakath Posted October 18, 2014 Author Share Posted October 18, 2014 No, I need to only return players which are near another object I stated. Link to comment
ZL|LuCaS Posted October 18, 2014 Share Posted October 18, 2014 No, I need to only return players which are near another object I stated. local x,y,z= getElementPosition ( localPlayer ) aa = createPed(0,x,y,z) local myTable= {} local x,y,z= getElementPosition ( aa ) for index,player in ipairs(getElementsByType("player")) do local a,b,c = getElementPosition ( player ) local distance = getDistanceBetweenPoints3D(x,y,z,a,b,c) if distance and (distance < 100) then myTable[index] = player outputChatBox(#myTable) end end Link to comment
DiSaMe Posted October 19, 2014 Share Posted October 19, 2014 # operator returns the index before the first index with a nil value. That is, table[#table+1] is nil. So if you don't put all values starting at index 1 and incrementing by 1, # returns the index before the first gap. For example, if you set values at indices 1, 2, 6, 8, 9, then # will return 2 because table[3] is nil. Link to comment
Drakath Posted October 19, 2014 Author Share Posted October 19, 2014 I see but I can't figure out how to make a work around for this. If I use table.maxn, it returns the right number but table.random doesn't work. Any help with the code would be appreciated. Link to comment
Saml1er Posted October 19, 2014 Share Posted October 19, 2014 You can do this with meta methods ( __index and _newindex) Check ixjf post. viewtopic.php?f=91&t=80425 If you use that code then when ever you get the length of table using # it will return __n and if you see carefully you will find another table __data where all of your data is kept. For more metatable events: http://lua-users.org/wiki/MetatableEvents Link to comment
Drakath Posted October 19, 2014 Author Share Posted October 19, 2014 Ahh, I can't get it to work. Getting the total number isn't hard but how can I make it work with table.random function table.random ( theTable ) return theTable[math.random ( #theTable )] end Link to comment
Anubhav Posted October 19, 2014 Share Posted October 19, 2014 function table.random ( theTable ) return theTable[ math.random ( 1, #theTable ) ] end Link to comment
Drakath Posted October 19, 2014 Author Share Posted October 19, 2014 Lol, it won't work. Didn't you read what CrystalMV posted? Link to comment
Saml1er Posted October 19, 2014 Share Posted October 19, 2014 You got this wrong. local __n = 5-- suppose table size is 5 but your table has 1,3,5,9,10 indexes local ran = math.random (__n) -- but this will return number between 1-5 it can also be 2 or 4 which is not your table index There's no possible way to do this other than a loop but we'll optimize the code by using a single loop rather than using 2. I found something useful on a forum. I modified his code a little. Here, Post by Robin: Also, a better way of dealing this is to keep another list with the keys: Code: local function generate_key_list(t) local keys = {} for k, v in pairs(t) do keys[#keys+1] = k end return keys end then you can do: Code: local keys = generate_key_list(myTable) local randomValue = myTable[keys[math.random(#keys)]] Link to comment
Drakath Posted October 19, 2014 Author Share Posted October 19, 2014 if table.maxn(myTable) > 0 then local keys = generate_key_list(myTable) local random = myTable[keys[math.random(#keys)]] outputChatBox(random) end I'm going to test it now but I have a couple of questions. 1. Is table.maxn okay to see if there are any players in the table? 2. If I use this client-side triggered by server, would every client receive an output of a different player or the same player? Link to comment
Saml1er Posted October 19, 2014 Share Posted October 19, 2014 1. Yes. 2. They will get different output since math.random exists for this purpose. Link to comment
Drakath Posted October 19, 2014 Author Share Posted October 19, 2014 Seems to work fine. Thank you Link to comment
ixjf Posted October 19, 2014 Share Posted October 19, 2014 (edited) Simply replace "myTable[index] = player" with "table.insert ( myTable, player )" in your original code and get rid of all the crap you've been told to do. Edited October 19, 2014 by Guest Link to comment
Saml1er Posted October 19, 2014 Share Posted October 19, 2014 Simply replace "myTable[index] = player" by "table.insert ( myTable, player )" in your original code and get rid of all the crap you've been told to do. I think I completely misunderstood him. Your code makes complete sense since table.insert will take care of index [#myTable+1] = v. Btw whatever I wrotr was for string keys. If you're using index keys then do what ixjf said. Link to comment
Moderators IIYAMA Posted October 21, 2014 Moderators Share Posted October 21, 2014 To be honest the table.insert has it's benefits, but not in this code. Table.insert is a little bit slower(because it is a function), so you won't have benefit from it now. Benefit of table.insert is that you can add data at fields without overwrite other fields. myTable = { "hi1","hi2","hi4" } table.insert(myTable,3,"hi3" ) outputChatBox = outputChatBox or print for i=1,#myTable do outputChatBox(myTable[i]) end Result: hi1 hi2 hi3 hi4 So do what you want to use and know the differences between them! 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