-- MODULESCRIPT CODE
local weaponManager = {}
local playerData = {}
local pFormat = {
player = "",
weapon = "";
}
function weaponManager.createPlayerData(player, weapon)
pFormat["player"] = player
pFormat["weapon"] = weapon
table.insert(playerData, pFormat)
end
Each time you call the function, the pFormat table is being changed, you should try putting the table in the function so you’re not changing the same one over and over but making a new one each time
or you could do this
-- MODULESCRIPT CODE
local weaponManager = {}
local playerData = {}
local pFormat = {
player = "",
weapon = "";
}
function weaponManager.createPlayerData(player, weapon)
local clone = table.clone(pFormat)
clone["player"] = player
clone["weapon"] = weapon
table.insert(playerData,clone)
end
In Lua all objects are not actually objects - rather a pointer towards memory where the object is held.
When you change pFormat, you’re changing the object in memory then inserting that memory address into the table, and since you they are all just references to the same object, changing one changes all of them.
Here is a fixed version of your code:
-- MODULESCRIPT CODE
local weaponManager = {}
local playerData = {}
function weaponManager.createPlayerData(player, weapon)
table.insert(playerData, { -- creating a new object
player = player,
weapon = weapon
})
end
If you’d like to maintain this odd “pFormat” object (which is silly, Luau has a type system, try that), you can use this instead:
-- MODULESCRIPT CODE
local weaponManager = {}
local playerData = {}
local pFormat = {
player = "",
weapon = "";
}
function weaponManager.createPlayerData(player, weapon)
pFormat["player"] = player
pFormat["weapon"] = weapon
table.insert(playerData, table.clone(pFormat))
end
Here we use table.clone to shallow-copy the table.