Datastore with tables

I have never really used datastores with tables before. i was wondering how i can save and load the data easily.

here is my current script:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("emp43")

local data = {
	["level"] = 1,
	["kills"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	for name, value in pairs(data) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end
	
	local s, e = pcall(function()
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)
	
	if s then
		print("Getting Data For: " .. Player.Name)
	elseif e then
		warn("Error Getting Data For: " .. Player.Name)
	end
	
	if DataFromStore then
		for name, value in pairs(DataFromStore) do
			Player.leaderstats[name].Value = value
		end
		print("Data Loaded For: " .. Player.Name)
	else
		print("Created New Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}
	
	for name, value in pairs(Player.leaderstats:GetChildren()) do
		table.insert(DataForStore, value)
		DataForStore[value.Name] = value.Value
		print(unpack(DataForStore))
	end
	
	TestDataStore:SetAsync("uid-" .. Player.UserId, DataForStore)
end)

the error i am getting at the moment is: 1 is not a valid member of folder on line 36

14 Likes

On this line:

Player.leaderstats[name].Value = value

You’re indexing a ValueBase with the name of the index of this value however when you save it here:

	local DataForStore = {}
	
	for name, value in pairs(Player.leaderstats:GetChildren()) do
		table.insert(DataForStore, value)
		DataForStore[value.Name] = value.Value
		print(unpack(DataForStore))
	end

You’re adding it to the table which you’re saving with it’s index as it’s name and the value as it’s value but you’re also table.inserting which can give it an index of 1 (array terms)
I’d just remove table.insert(DataForStore, value) and see how it goes.

3 Likes

it didn’t error but neither did it get the data

1 Like

When it saved did it print correctly?

1 Like

yes, it did. i have no idea what is wrong with it

1 Like

Is studio access to API services enabled?


I tested this and it all works perfectly fine for me:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TestDataStore = DataStoreService:GetDataStore("emp43")

local data = {
	["level"] = 1,
	["kills"] = 0
}

Players.PlayerAdded:Connect(function(Player)
	local DataFromStore = nil
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	for name, value in pairs(data) do
		local new = Instance.new("NumberValue")
		new.Name = name
		new.Value = value
		new.Parent = leaderstats
	end
	
	local s, e = pcall(function()
		DataFromStore = TestDataStore:GetAsync("uid-" .. Player.UserId)
	end)
	
	if s then
		print("Getting Data For: " .. Player.Name)
		if DataFromStore then
			for name, value in pairs(DataFromStore) do
				Player.leaderstats[name].Value = value
			end
			print("Data Loaded For: " .. Player.Name)
		else
			print("Created New Data For: " .. Player.Name)
		end
	else 
		warn("Error Getting Data For: " .. Player.Name)
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local DataForStore = {}
	
	for name, value in pairs(Player.leaderstats:GetChildren()) do
		DataForStore[value.Name] = value.Value
	end
	
	local success = pcall(TestDataStore.SetAsync, TestDataStore, "uid-" .. Player.UserId, DataForStore)
	
	if success then
		print("Successfully Saved Data For: " .. Player.Name)
	end
end)
21 Likes

still not saving, i went into the datastore editor and i saw both the values were 0 even when set to 999 in game

1 Like

How are you setting the values? Are you changing the values on the client?

1 Like

i am changing them with explorer. is that client?

1 Like

You see this button? Press it

Now you’re on the server

Go edit the values and the datastore will see and save the changes

7 Likes

ah, ok. it works now. thank you for your help

4 Likes