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

How would I save and obtain a table in a Datastore?

local datastore = game:GetService('DataStoreService'):GetDataStore('Codes')
local saveTable = {}

game.Players.PlayerAdded:connect(function(plr)
	local key = '_' .. plr.UserId
	local tbl = datastore:GetAsync(key, saveTable)
end)

game.Players.PlayerRemoving:connect(function(plr)
	local key = '_' .. plr.UserId
	
	datastore:SetAsync(key, saveTable)
end)

I am making a datastore for twitter codes, and this is just a small part of the script. The codes save to the table, but not to the datastore. So when I put in a code, it goes in the table, I leave and come back, then the code isn't in the table anymore. Help is highly appreciated, thanks! :D

2 comments

0

Player.DataReady has to see with DataPersistence, not Data stores.

0

I updated it but it still doesn't save nor' load

1 answer

No need for that additional table.

Try this:

game.Players.PlayerAdded:connect(function(plr)
	local key = "_" ..plr.userId
	local SavedData = DataStore:GetASync(key)

	if SavedData then
		-- Retrieve data here by doing this.
		game.ReplicatedStorage.Code1.Value = SavedData[1]
	game.ReplicatedStorage.Code1.Value = SavedData[2]
	else
		-- Set a sync
		local saveValues = {game.ReplicatedStorage.Code1.Value, 				game.ReplicatedStorage.Code2.Value} -- And so on,
		DataStore:SetASync(key, saveValues)
	end
	
end)

game.Players.PlayerRemoving:connect(function(plr)
	local key = "_" ..plr.userId
	local saveValues = {game.ReplicatedStorage.Code1.Value, 					game.ReplicatedStorage.Code2.Value} -- And so on,
	DataStore:SetASync(key, saveValues)
end)

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