SetAsync from a different script, if there are multiple values

Hello dear developers!
I’m trying to make a script that changes a player’s datastored value even if it’s not in game. It worked if there was one value saved in a datastore but it doesn’t if there are multiple. Here’s how it looks like

local datastore = game:GetService("DataStoreService")
local statsstore = datastore:GetDataStore("StatsInfo")
local playername = "Mimas18Hun"
local key = "Player_"..game.Players:GetUserIdFromNameAsync(playername)
local playerid = game.Players:GetUserIdFromNameAsync(playername)
local First = (statsstore:GetAsync(key)[1]) 
local newvalueinnumber = 50
local newdata = {First = newvalueinnumber}

local succ, err = pcall(function()
	statsstore:SetAsync("Player_"..playerid, newdata)
end)

Thanks in advance for any sensible answers.

Can you explain this a bit more thoroughly? Are you saying that if this;

…line of code has more than one value {"One", "Two"} then it doesn’t save?

So, in that line I was trying to change the first datastored value’s value
image

like from this ^ to this ˇ

image

but it always turned into this ˇ

image

image

That is an array, but {First = newvalueinnumber} is a dictionary. Dictionaries are made using anything but numbers, like so;

local MyDictionary = {
    MyData1 = 50,
    MyApples = -1,
    MyName = "abc"
}

You cannot use First as that is not a number, and you are not changing only the first value, you are changing the entire thing, so;

local Data = {500, 0}

will become;

local Data = {First = 50}

because you’ve only given one number in the table, you aren’t changing just the first element.

You need to give the entire table to do what you are asking, so;

local PlayerData = Datastore:GetAsync(Key)

--arrays use numbers for keys, so PlayerData[1] is the first number
--and we set that to 50
PlayerData[1] = 50

Datastore:SetAsync(Key, PlayerData)

and no, you can’t just copy this code, there’ll be errors because I’m using different variable names, and also probably because you’ve changed the data to a dictionary

Thank you so much!
This is very helpful :slight_smile: