Jump to content

Table indexes


Drakath

Recommended Posts

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
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
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

# 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

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
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
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

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

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...