Hello!
I have a Datastore script which I am going to share later in this post. My problem is that I am not sure how to switch over to :SetAsync or when to use it and how to use it across servers (like a gifting system).
My script basically works like this:
Users can :SetAsync by invoking the server at any time (limit of 10 per minute which is the limit when users are added iirc). User data automatically saves when player leaves.
The problem is that I am really not sure how :SetAsync works when updating a specific value when my script saves as a table.
Hereâs an example of what a userâs data might look like:
local defaultData = {
['Coins'] = 0;
['Inventory'] = {
['Accessories'] = {};
['Gear'] = {};
['Trails'] = {};
['Crates'] = {};
};
['Passes'] = {
['Premium'] = false;
};
['Rank'] = 'AlphaTester';
['Permission'] = 1;
['BanInfo'] = {
['Banned'] = false;
['BanReason'] = 'N/A';
['UnbanTime'] = 'N/A';
};
['Settings'] = {
['UIPrimaryColour'] = {
['R'] = 86;
['G'] = 146;
['B'] = 127;
};
['UISecondaryColour'] = {
['R'] = 255;
['G'] = 255;
['B'] = 255;
};
['UIFont'] = 'SourceSans';
['GlobalShadows'] = true;
['PlayerMode'] = 'Visible';
['ResetKeybind'] = 'R';
};
}
When converted to instances:
Function to save, it reconstructs the playerâs data from instances to a table:
local function saveData(player)
local key = player.UserId
local playerFolder = player:FindFirstChild('PlayerData')
local odsFolder = player:FindFirstChild('ODSData')
local obbiesData, winsData = odsFolder:FindFirstChild('Obbies').Value, odsFolder:FindFirstChild('Wins').Value
function convertInstanceToData(currentFolder)
local returnedData = {}
for i,v in pairs(currentFolder:GetChildren()) do
if v:IsA('Folder') then
returnedData[v.Name] = convertInstanceToData(v)
else
returnedData[v.Name] = v.Value
end
end
return returnedData
end
local constructedTable = convertInstanceToData(playerFolder)
local successful, errorMessage = pcall(function()
dataStore:SetAsync(key, constructedTable)
winsDataStore:SetAsync(key, winsData)
obbiesDataStore:SetAsync(key, winsData)
end)
if not successful then
warn(tostring(errorMessage))
end
end
TIA for any help!
Oh also little PS, winsDataStore and obbiesDataStore are in different datastores as they are ordereddatastores, not standard ones.