Table being passed as pointer?

I have this issue where a table of default values that is NOT supposed to be changed is indeed being changed.

at the head of my script i have a table that serves as default values like so:

local defaultDamageValues = {DamageEnabled = true, DamageDay = 0, DamageNight = 0}

I also have a table to store data for each player, I create an entry for the player when they join.

PlayerUtilityService.PlayerDamageStatus = {}

Then later on, I have a method to increment the values so the player might take some damage incrementally ( this is for a vampire kinda thing).
Here is the method:

function PlayerUtilityService:IncrementDamageValues(player, params)

    if not player or not params then return end

    print("damage default 1", defaultDamageValues)

    if PlayerUtilityService.PlayerDamageStatus[player.UserId] == nil then
        PlayerUtilityService.PlayerDamageStatus[player.UserId].DamageEnabled = defaultDamageValues
    end

    print("damage default 2", defaultDamageValues)

    PlayerUtilityService.PlayerDamageStatus[player.UserId].DamageDay += params.DamageDay
    PlayerUtilityService.PlayerDamageStatus[player.UserId].DamageNight += params.DamageNight

    print("damage default 3", defaultDamageValues)

end

As you can see, if the player has nothing in their table, I will populate it with the default values set at the head of the script. NOw that they have some default values ready, i then increment the values sent in params. But the issue is that when i am incrementing values, it is ALSO setting the defaultDamageValues table!

Why is this happenieng and how can i do it differently? I dont understand how a pointer was set in this case and why the defaultDamageValues table is being set.

To copy tables without simply passing them as a reference, you’ll need to actually go through the table and recreate each key-value pair in a new table. You can use the methods featured in this article: Tables | Roblox Creator Documentation

1 Like