I know it’s kind of a simple question, but I just found out I shouldn’t be using SetAsync. I tried looking around DevForum and Roblox Documentation, but none of them have been really all that clear. I tried doing it myself, but whenever I try to get the data, it returns as nil. If someone could modify my script, I would really love that. Thanks!
Script:
--Load Data
local upgradedata
local success, errormessage = pcall(function()
upgradedata = upgradeData:GetAsync(playerUserId)
end)
if success then
if upgradedata then
for i, saveWhat in pairs(Upgrades:GetChildren() and Upgrades:GetDescendants()) do
for dataValue, v in pairs(upgradedata) do
if dataValue == saveWhat.Name then
saveWhat.Value = dataValue
end
end
end
end
end
You can use SetAsync, As the docs say you should only use UpdateAsync:
In cases where another game server updated the key in the short timespan between retrieving the key’s current value and setting the key’s value, GlobalDataStore:UpdateAsync() will call the function again to ensure that no data is overwritten.
This is a pretty uncommon case for game data to be written for a specific player on two different servers, since they can’t be in both servers at once.
Your for loop is wrong though, :GetDescendants() will include all children so please remove :GetChildren(), the and keyword does not combine tables, it is merely selecting :GetDescendants() as the last true-thy value
for i, saveWhat in pairs(Upgrades:GetDescendants()) do
I did this, it still returns nil every time I join.
local data = {}
for i, v in ipairs(player.Upgrades:GetChildren()) do
data[v.Name] = v.Value
end
for i, v in ipairs(player.Upgrades:GetDescendants()) do
data[v.Name] = v.Value
end
print(data)
local success, errormessage = pcall(function()
upgradeData:UpdateAsync(playerUserId, function(oldData)
local previousData = oldData or {DataId = 0}
if data.DataId == previousData.DataId then
return data, data:GetUserIds(), data:GetMetadata()
else
return nil
end
end)
end)
UpdateAsync is for saving data based on old data (like += opposed to setAsync being like =).
What do you mean it returns nil when you join? Are you just trying to say the data isn’t saved and you get nil from :GetAsync()?
Looks like your current function will only save data if the previous DataId matches, otherwise it will delete your data. I wouldn’t do the id check since your datastore already uses the playerUserId as a key.
The posted snippet doesn’t print out the errors but I have a feeling you are getting errors from data:GetUserIds(), data:GetMetadata() since these are functions for the DataStoreKeyInfo that you’ve neglected to include as a parameter