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

How to load and save a table?

Im not sure how to do so... here is my attempt

local DATA = game:GetService("DataStoreService"):GetDataStore("lol")

local randomTable = {
"1",
"2"
}

DATA:SetAsync("key", randomTable)

DATA:UpdateAsync("key", function(OldValue)
print(OldValue[2])
return OldValue 
end)

game.Players.PlayerAdded:connect(function(Player)
	wait(2)
	if DATA:GetAsync(Player.userId) then
		for i,v in pairs(randomTable:GetChildren()) do
		randomTable[i] = DATA:GetAsync(Player.userId)[i]
		randomTable[i] = DATA:GetAsync(Player.userId)[i]
		end
	end
end)

1 comment

0

GetAsync is an expensive request so you should only do it once per thread. It's hard to help since the code makes no sense. http://wiki.roblox.com/index.php?title=Saving_Player_Data

1 answer

Think of using DataStores as like your own personal, super big storage closet. This storage closet is special, because you can make keys, and a new storage door will be added when you make a key.


A key is a string. A key can be pretty much any string you want. If you want to save data for each player, you can use their UserId as a key. Normally you concatenate their UserId with another string, in case you want to use their UserId later to save data in a different 'storage closet'.

--// Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--// Key Bases
local headColorKey = "HeadColor_" -- can be anything. This is a base.

--\\ Player Added
Players.PlayerAdded:Connect(function(player)
	local key = headColorKey..player.UserId
end)
The key, in the code above, could be HeadColor_2353463, if the player who joined userId was 2353463. So they will have a special key, different from any else's.

Now we have to get a data store, and load data using GetAsync().

--// Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--// DataStore
local globalDS = DataStoreService:GetGlobalDataStore() -- gets main DS

--// Key Bases
local headColorKey = "HeadColor_"

--\\ Player Added
Players.PlayerAdded:Connect(function(player)
	local key = headColorKey..player.UserId
	local data = globalDS:GetAsync(key) -- is there a 'door' made with the key yet?
end)

If there isn't any data, we have to make the data and the data storage! AKA: Making the door. We do this using SetAsync().

--// Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--// DataStore
local globalDS = DataStoreService:GetGlobalDataStore() -- gets main DS

--// Key Bases
local headColorKey = "HeadColor_"

--\\ Player Added
Players.PlayerAdded:Connect(function(player)
	local key = headColorKey..player.UserId
	local data = globalDS:GetAsync(key)
	if not data then -- if no data, AKA: No door.
		data = BrickColor.Random().Number
		globalDS:SetAsync(key, data) -- assign data to key, AKA: Putting data in the door
	end
end)

This example is almost done! We load data, and if the data does exist we make it! Now we just have to use it.

--// Services
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")

--// DataStore
local globalDS = DataStoreService:GetGlobalDataStore() -- gets main DS

--// Key Bases
local headColorKey = "HeadColor_"

--\\ Player Added
Players.PlayerAdded:Connect(function(player)
	local key = headColorKey..player.UserId
	local data = globalDS:GetAsync(key)
	if not data then -- if no data, AKA: No door.
		data = BrickColor.Random().Number
		globalDS:SetAsync(key, data) -- assign data to key, AKA: Putting data in the door
	end
	player.CharacterAdded:Connect(function(character)
		local head = character:WaitForChild("Head")
		head.BrickColor = BrickColor.new(data)
	end)
end)
The above should make each players' head a random color once, and never change the color until the dataStore is reset, or the key is changed.

I hope my small tutorial helped!

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