Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

SetAsync Is Not Saving the Data To My DataStore?

Hey, I am having problem with Saving Data.

This is the code that saves the data, When I press a GUI Button, the Remote Event Fires. I am trying to save the "Uses"'s Value I get the Print "Save Data Firing", but when I look the DataStore Editor, the Data hasn't been saved.

The Data Loading works fine, anyone knows what's wrong?

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

game.ReplicatedStorage.saveData.OnServerEvent:Connect(function(player)
	print("Save Data Firing")
	local theTable = {}
	table.insert(theTable, player.Uses.Value)
	local success, failed = pcall(function()
		DataStore:SetAsync(player.UserId, theTable)
	end)
		if success then
			print("Succesfuly Saved")
		else
			print("Data Couldn't Save")	
		end
end)

2 comments

0

Do you have API turned on inside studio?

0

Yes, the data loads fine, it just that it doesnt save

1 answer

Mayby try this:

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

game.ReplicatedStorage.saveData.OnServerEvent:Connect(function(player)
    print("Save Data Firing")
    local theTable = {}
    table.insert(theTable, player.Uses.Value)
    local theTable = game:GetService("HttpService"):JSONEncode(theTable)
    local success, failed = pcall(function()
        DataStore:SetAsync(player.UserId, theTable)
    end)
        if success then
            print("Succesfuly Saved")
        else
            print("Data Couldn't Save") 
        end
end)

Loading Script:

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

game.ReplicatedStorage.loadData.OnServerEvent:Connect(function(player)
	print("Load Data Firing")
	local success, failed = pcall(function()
		theTable = game:GetService("HttpService"):JSONDecode(DataStore:GetAsync(player.UserId))
	end)
	if success then
		print("Succesfuly Loaded")
	else
		print("Data Couldn't Load") 
	end
end)
--Do the rest

Hope this helps :) I know you already have the loading script, but you need to decode it.

3 comments

0

Hey Mate, Thanks for reply, is there anyway to do this without the JSONDecode, I looked on the forum about it, i don't seem to understand it, or if you could explain it to me, as im not a big fan of using code that I don't understand!

0

JSON decode decodes the JSON you encoded. Since we encoded the JSON, To convert it to a table again, you need JSON Decode.

0

To get the JSON string into a table, we might need JSON to convert it to string table, then when you are reciving the JSON, to get it to table, we decode it. JSON is a string, not a table which supports DataStores.

This discussion is preserved as it was. New answers, comments, and votes are closed.