Jump to content

[Solved] Problem with tables


ManeXi

Recommended Posts

I need to add a player to a table as index and value, I have tried this:

            totalPlayerInEvent[player] = player 
            for _, player in ipairs(totalPlayerInEvent) do  
                triggerClientEvent(player, "joinEvent", root) 
            end 

to clear "player" is the guy who triggered the event that i'm working with, nothing is wrong with it.

instead if I use table.insert it works but badly it puts default index(1, 2, 3...) and I need to use [player] as index to be able to manipulate the value (in other words to be able to remove it)

PD: I didn't get any error in debug log

Edited by Guest
Link to comment

Use pairs in this case, You are storing the player as an index and you inserted the same value as the index, why ?

You can simply retrieve the stored player element just like that

  
for player in pairs ( totalPlayerInEvent ) do 
    triggerClientEvent ( player, "joinEvent", root ) 
end 

Link to comment
Use pairs in this case, You are storing the player as an index and you inserted the same value as the index, why ?

You can simply retrieve the player element just like that

  
for player in pairs ( totalPlayerInEvent ) do 
    triggerClientEvent ( player, "joinEvent", root ) 
end 

Your solution amazingly worked, but I have alot of doubts about it.

#1 Why does work only with pairs?

#2 When I use: totalPlayerInEvent[player] = player what's the index and what's the value? (firstly I thought Index was just the player, and the value player(element), now my mind has blown up.)

#3 the "player" of the "for" what does refer? to the index? element? value?

#4 If I use totalPlayerInEvent[player] = nil will be removed from the table, right?

Try using getPlayerName(player) as the index. Then later on, use getPlayerFromName(i) to retrieve the player.
I thought in that but if any player changes nickname throught it something might get bugged and probably any exploit would appier
Link to comment

#1: ipairs sorts the table by index (integer) and goes from 1 to the highest number before an empty index.

local table1 = { "hello", "my", "name" } 
local table2 = { [1] = "hello", [2] = "my", [4] = "name" } 
local table3 = { ["hello"] = 1, ["my"] = 2, ["name"] = 3 } 
  
for index, value in ipairs ( table1 ) do outputChatBox ( value ) end  
-- "hello" "my" "name" (sorted) 
  
for index, value in ipairs ( table2 ) do outputChatBox ( value ) end 
-- "hello", "my" (sorted, max index was 2 because of the nil value at [3]) 
  
for index in ipairs ( table3 ) do outputChatBox ( index ) end 
-- NOTHING 
  
for index in pairs ( table3 ) do outputChatBox ( index ) end 
-- "name", "hello", "my" (not sorted) 

But pairs doen't look at the index.

It just iterates the table without looking at the index or sorting it.

#2: The index would be player, the value would be player. Why has your mind blown up? o.O

#3: index

#4: Yes

You could remove from the table with integer index too.

for i=1, #table1 do 
    if table1[i] == yourvalue then 
        table.remove ( table1, i ) 
        break 
    end 
end 

Edited by Guest
Link to comment
local table3 = { ["hello"] = 1, ["my"] = 2, ["name"] = 3 } 
for index, value in pairs ( table3 ) do outputChatBox ( value ) end 
-- "name", "hello", "my" (not sorted) 

@Bonus, you did a mistake, the values of pairs function are 1, 2 and 3

and your code will output:

> 1

> 3

> 2

( not sorted of course )

"hello", "my" and "name" are the keys of the table3 not the values.

So, the correct code is:

local table3 =  
    { 
        ["hello"] = 1,  
        ["my"] = 2, 
        ["name"] = 3  
    } 
     
for key in pairs ( table3 ) do 
    outputChatBox ( key)  
end 
-- "name", "hello", "my" (not sorted) 

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