Table in ModuleScript replacing values with new value

I’m trying to create a dynamic “global” table in which other server scripts can access.

Inserting into the table works, however, all the values in that ModuleScript table keeps getting replaced with the newest value. For example:

# Module Script
[Table index] [Name] [Weapon]

What I want:
[1] [John] [Axe]
[2] [Chloe] [Sword]
[3] [Patty] [Bat]

What happens:
[1] [Patty] [Bat]
[2] [Patty] [Bat]
[3] [Patty] [Bat]

Here’s my code:

-- 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
-- SERVERSCRIPT CODE
local WeaponManager = require(ServerStorage.WeaponManager)

WeaponManager.createPlayerData("John", "Axe")
WeaponManager.createPlayerData("Chloe", "Sword")
WeaponManager.createPlayerData("Patty", "Bat")

print(WeaponManager.playerData[1]) --> [Patty, Bat]
print(WeaponManager.playerData[2]) --> [Patty, Bat]
print(WeaponManager.playerData[3]) --> [Patty, Bat]

Any clues?

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
-- SERVERSCRIPT CODE
local WeaponManager = require(ServerStorage.WeaponManager)

WeaponManager.createPlayerData("John", "Axe")
WeaponManager.createPlayerData("Chloe", "Sword")
WeaponManager.createPlayerData("Patty", "Bat")

print(WeaponManager.playerData[1]) --> [John, Axe]
print(WeaponManager.playerData[2]) --> [Chloe,Sword]
print(WeaponManager.playerData[3]) --> [Patty, Bat]
1 Like

Add table.clone(pFormat) instead of pFormat to the playerData table so something like this table.insert(playerData, table.clone(pFormat))

1 Like

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.

1 Like

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