I’m creating a system that uses tables of variables that are all based off a single template.
As these same tables are used multiple times in multiple scripts, the obvious answer to me is to stick them in a module script.
However, every variable I attempted to create seems to be refencing the template instead of creating a new table.
The following code is in a module script:
local LOOT_TABLE_TEMPLATE ={
ID =0;
}
Loot_Tables ={
Default =LOOT_TABLE_TEMPLATE;
Test =LOOT_TABLE_TEMPLATE;
NoItems =LOOT_TABLE_TEMPLATE;
}
Loot_Tables.Default.ID =1
Loot_Tables.Test.ID =2
Loot_Tables.NoItems.ID =3
return Loot_Tables
Printing “Loot_Tables” from above shows all three table’s "ID"s are 3, instead of the actual number each was separately assigned.
I’ve been unable to find any documentation, info, or even mention concerning this behavior at all.
Though I may be missing something, I’m at a complete loss as to how to fix it.
Setting a variable or index to a table gives a reference to that table instead of cloning. Its like referencing an instance, you don’t clone it, you take a reference. You can use table.clone to get a copy instead.
local function getTemplate()
return {
ID = 0;
}
end
It creates a reference because it’s supposed to do that. It’s the exact same table. It’s just like a part. It’s still the same part even though you assign two different variables to it.