Does table.insert copy the value before inserting it into a table

My question is really just this. Let’s say I have a data structure

local player = {
    kills = 0,
    xp = 0,
    team = "Mango"
}

I have another system that needs this player data, but the original address of it since only 1 unique player data structure should exist for each player.

However, when I do for example,

table.insert(SomeTeamPlayers, player)

Will this copy the player dictionary or use the original dictionary pointer?
I should be able to do this correct?

SomeTeamPlayers[table.find(SomeTeamPlayers, player)] == player

However, this does not work for some reason. I cannot figure out if its table.find not comparing the memory addresses or if table.insert is copying and inserting the value

In Luau, when you use table.insert(SomeTeamPlayers, player), it doesn’t create a copy of the player table. Instead, it stores a reference to the original table in the SomeTeamPlayers table.

This means that if you modify the player table later (for example, changing player.kills or player.xp), those changes will be reflected in SomeTeamPlayers because both variables point to the same table in memory.

1 Like

Thank you, I believe it must be table.find then. If all goes bad I’ll just store a unique identifier into each player data structure and just compare that

No problem at all! By the way, this is my first message on the DevForum, so I appreciate your understanding. If you have any more questions or need further clarification, feel free to reach out. I’m here to help!

Will do. Welcome to the developer forums!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.