Problems with Datastore

I’m making custom accessories for character creation, where you put the custom accessory, it saves all the values ​​in a table that is in a JSON-encoded string value, this json is saved with datastore and when loaded it is decoded so that the items can be placed accordingly.

I managed to make the accessories and managed to make the table, it’s almost there
However, the rescue is not working and I already tried to do my best.

“onPlayerAdded”, inside game.Workspace

local DataStore = game:GetService("DataStoreService"):GetDataStore("AcessoriesSave")


game.Players.PlayerAdded:connect(function(player)
	
	
		local stats = Instance.new("StringValue",player)
		stats.Name = "savestorage"
		
		local save1 = Instance.new("StringValue",stats)
		save1.Name = "Save1"
		local save2 = Instance.new("StringValue",stats)
		save2.Name = "Save2"
		local save3 = Instance.new("StringValue",stats)
		save3.Name = "Save3"		
		local save4 = Instance.new("StringValue",stats)
		save4.Name = "Save4"
		local save5 = Instance.new("StringValue",stats)
		save5.Name = "Save5"

	
	local key = "qhhQX6LE07sx9400-"..player.userId
	
	local savedValues = DataStore:GetAsync(key)
	
	if savedValues then
		--Save format: {points, coins}
		save1.Value = savedValues[1]
		save2.Value = savedValues[2]
		save3.Value = savedValues[3]
		save4.Value = savedValues[4]
		save5.Value = savedValues[5]
	else
		local valuesToSave = {save1.Value, save2.Value,save3.Value,save4.Value,save5.Value}
		DataStore:SetAsync(key, valuesToSave)
	end
	
	--
	save1.Changed:connect(function(Val)
                DataStore:SetAsync(key, Val)
	        end)
	--
	save2.Changed:connect(function(Val)
                DataStore:SetAsync(key, Val)
	        end)
	--
	save3.Changed:connect(function(Val)
                DataStore:SetAsync(key, Val)
	        end)
	--
	save4.Changed:connect(function(Val)
                DataStore:SetAsync(key, Val)
	        end)
	--
	save5.Changed:connect(function(Val)
                DataStore:SetAsync(key, Val)
        end)
	
end)

“playerLeaving”, game.Workspace

local DataStore = game:GetService("DataStoreService"):GetDataStore("AcessoriesSave")

game.Players.PlayerRemoving:connect(function(player)
	
	local key = "qhhQX6LE07sx9400-"..player.userId
	
	local valuesToSave = {player.savestorage.Save1.Value, player.savestorage.Save2.Value,
		player.savestorage.Save3.Value,player.savestorage.Save4.Value,
	player.savestorage.Save5.Value}
	
	DataStore:SetAsync(key, valuesToSave)
	
end)

“TheSaveEvent”, game.Workspace.Events.TheEvents

-- New Server Script
local event = Instance.new("RemoteEvent")
event.Name = "SaveEvent"
event.Parent = game.Workspace["Events"]["TheEvents"]
event.OnServerEvent:connect(function(player, liger,WhatToChange)
	
------------------- The Script.

player.savestorage[""..WhatToChange].Value = player.Character.TheAcessoriesValue.Value

-------------------


end)

“TheLoadEvent”, game.Workspace.Events.TheEvents

-- New Server Script
local event = Instance.new("RemoteEvent")
event.Name = "LoadEvent"
event.Parent = game.Workspace["Events"]["TheEvents"]
event.OnServerEvent:connect(function(player, liger,WhatToChange)
	
------------------- The Script.
	
player.Character.TheAcessoriesValue.Value = player.savestorage[""..WhatToChange].Value
	
end)

“Save1” Button

function onButtonClicked()
	script.Disabled = true
	local liger = game.Players.LocalPlayer.Character
	game.Workspace["Events"]["TheEvents"]["SaveEvent"]:FireServer(
		game.Players.LocalPlayer.Character,script.Parent.Name)
	wait(30.1)
	script.Disabled = false
end
script.Parent.MouseButton1Down:connect(onButtonClicked)

“Save1”, Button

function onButtonClicked()
	local liger = game.Players.LocalPlayer.Character
	
	if script.Parent.Parent.IsLoading.Value == "Nah" then
		
		game.Workspace["Events"]["TheEvents"]["LoadEvent"]:FireServer(
			game.Players.LocalPlayer.Character,script.Parent.Name)
		
		repeat wait() until liger.CharVariables.IsLoading.Value == "Si"
		-- Loading character.

		wait(1)
		
		------ Now do it.
		
		local HttpService = game:GetService("HttpService")
		local rs = game.ReplicatedStorage
		local player = game.Players.LocalPlayer
		local char = player.Character
		local strvalue = char.TheAcessoriesValue
		local ajdaj = strvalue.Value
		local json = HttpService:JSONDecode(ajdaj)
		
		if not json then 
			json = {}
		end
		
		for k in pairs(json) do
			rs.AcessoryFire:FireServer(json[k])
		end
		
	end
	
end

script.Parent.MouseButton1Down:connect(onButtonClicked)

I don’t know what the problem is, I already solved everything that I thought was a problem. Nothing is displayed on the console so I can’t identify what the problem is

When a stringValue changes you’re overwriting the Async with the string value. The array is then lost. Instead of setting async every time, save it to a cache array and make a routine that auto saves over time. You want to avoid reaching the SetAsync Limit.

Example:

local playerData = {save1.Value,...,saveN.Value}
dataStore:SetAsync(key, playerData)
while wait(120) do
   if player then   
      dataStore:SetAsync(playerData)
   else 
      break  
   end
end

Thanks for the tip, I’m using it now. But my problem has not yet been solved