Having problems with saving all data

All stats save perfectly, except for MaxStamina. If any of the stats are changed, MaxStamina doesn’t save. If only MaxStamina is changed, MaxStamina saves.

Really weird problem, I hope someone that reads this has a clue on how to solve it.

local Defaults = {
Strength = 0, 
Agility = 0,
Durability = 0, 
Toughness = 0, 
Special = 0, 
MaxStamina = 100
}

local function OnPlayerAdded(Player)
    local StatDS = DataStore2("Stats", Player)

    local function StatsUpdate(newValues)        
        local Stats = Player:WaitForChild("StatFolder")
        local StatArray = {"Strength", "Agility", "Durability", "Toughness", "Special", "MaxStamina"}
        local function Check(thing)
            for i, v in pairs(StatArray) do
                if v == thing then
                    return true
                end
            end
        end
        for i, v in pairs(newValues) do
            if Check(tostring(i)) then
                Stats[tostring(i)].Value = v
            end
        end
    end

    StatsUpdate(StatDS:GetTable(Defaults))
    StatDS:OnUpdate(StatsUpdate)
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)

I believe this is the only relevant part of the script but I can post the other parts too if needed.

Ok, so everything else is saving but max stamina. Just clarifying?

Yes, everything saves ok, however MaxStamina only saves if it’s the only value that changes.

I test by stopping the test and starting it again in studio.

edit: i can provide a gif of me changing the value and seeing how it interacts

Ok, let me review your code give me a sec.

Mhmm, nothing looks wrong with the code to me. Try turning on API references.

Okay, I tested some more and it seems it only saves any data that has changed, and old data that didn’t change gets defaulted. Sorry for not checking thoroughly before

It’s ok, I actually noticed that in your code. No worries! Glad it works.

Wait, which part of the code is faulty? I noticed the problem but I don’t see it in my code lol

Okay, I tested some more and it seems it only saves any data that has changed, and old data that didn’t change gets defaulted. Sorry for not checking thoroughly before. This is what you said.

The max stamina did not change, therefore it was set back to the default value of 100.

Yeah, how do I make it also save data that didn’t change? I wasn’t really clear, my bad

A little confused, no need to save data if it did not change. The script will do that for you.

But it’s still a problem though. I gave myself 15 strength and it saved, I went back and gave myself 15 durability to have 15 str and dura, and only the durability saved. How can I make it also save the past strength?

Or rather, what am I doing wrong

Found out the problem. I was using :Set() when changing my stats and not :Update(), therefore overwriting the entire data table.