DataStoreService "Overwrite"

Lets say that I have in a data store a key and a value. Lets say that the key is “x” and the value is equal to 7 for example. If I later on ran a :SetAsync() command to set “x” to 8, will it overwrite the original x = 7 or would it simply add on to what is inside the data store and give me a random value out of the two when calling :GetAsync()? I am aware that it is bad practice to overwrite instead of :UpdateAsync(). However, is there potential for the DataStoreService functioning in a game with how I am trying to use it?

Edit: Thank you for all of the help.

Completely overwrites, I am not sure why the behavior would be

as this is not useful behavior and will just cause more headaches down the line.

2 Likes

Think of a datastore as a dictionary (which it basically is). To increment the value held by a dictionary key, you have to do something along the lines of “dictionary[“key”] = dictionary[“key”] + number”. SetAsync is the equivalent of just setting the value of that key, “dictionary[“key”] = value”.

2 Likes
local Data = {}
Data = BackPack:GetAsync(player.UserId)
Data[ItemName] = 1
BackPack:SetAsync(player.UserId, Data)
Data = nil

i use this method

so it puts the data store to a table
then add a key and value in it
and after that Save it!

1 Like

Are you wrapping that in a pcall in your script or is that how you write it. If thats how that is then you should wrap it in a pcall.

yup i wrap it on a pcall, sorry i forgot to write it lol

heres my script

local success, err = pcall(function()
    local Data = {}
    Data = BackPack:GetAsync(player.UserId)
    Data[ItemName] = 1
    BackPack:SetAsync(player.UserId, Data)
    Data = nil
end)
if err then
     warn(err)
     --gui that warns player that data is not saved
     --trying to save it again
end
1 Like