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)
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