I have a table that contains the names of the players that were in the game about 3 minutes ago
I have a second table which contains the names of the players that are currently in the game
How can I compare these two tables and save whoever’s name that pops in both the tables into a whole another table? In other languages this is rather simple but I couldn’t figure out how in lua.
Do a nested loop. Firstly create an empty table for the concurrent players. Then loop through all of the players in the table containing the players from 3 minutes ago, and in this every iteration, do another loop through the current players. In this loop, simply put an If statement to check if the player from the old table == to the one currently iterated in the current players. If it does, assign the player to the empty table from beginning and break the loop. When it finishes, you will have the table from beginning filled with only the consecutive players.
Iterate through the old table using a for loop and use table.find to check if the player exists in the other table. If found then insert the player into a new table. For efficiency consider sorting both tables manually or using table.sort before using table.find since it’s a linear search.
Table.find is slower than creating the loop manually, and it also removes you the ability to eventually have more parameters to search with. There is literally no reason to use table.search other than slightly better readability, not even mentioning it only works in sequentially integer indexed tables
OP has tables of player names so the time complexity is the same? If he were to have a table of sorted player IDs then a binary search algorithm for example would be faster sure