How to load and save data with DataStore with tables?

I have a problem, and it is that I want to learn how to save values ​​with tables, but I honestly don’t know how saving and loading data with tables works.

I guess to save, it is like this:

local datastore = game:GetService("DataStoreService")
local NeedsDataService = datastore:GetDataStore("NeedsData")

players.PlayerAdded:connect(function(plr)
	local Folder = Instance.new("Folder")
	Folder.Name = "DataPlayer"
	Folder.Parent = plr

	local NeedsPlayerStats = Instance.new("Folder")
	NeedsPlayerStats.Name = "NeedsPlayerStats"
	NeedsPlayerStats.Parent = Folder

	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Parent = NeedsPlayerStats

	local Thirst = Instance.new("IntValue")
	Thirst.Name = "Thirst"
	Thirst.Parent = NeedsPlayerStats
end)

players.PlayerRemoving:Connect(function(plr)
	local NeedsPlayer = {
		Hunger = plr.DataPlayer.NeedsPlayerStats.Hunger.Value;
		Thirst = plr.DataPlayer.NeedsPlayerStats.Thirst.Value;
	}
	NeedsDataService:SetAsync(plr.UserId, NeedsPlayer)
end)

But how do I load the data?

3 Likes

Use GlobalDataStore:GetAsync().
In your case, local NeedsPlayerDataStore = NeedsDataService:GetAsync(plr.UserId) should give you the NeedsPlayer table specific to an user.

Now I’m confused, I don’t know how to accommodate it or what to do

	local HungerValue = Instance.new("IntValue")
	HungerValue.Name = "Hunger"
	HungerValue.Parent = NeedsPlayerStats

	local ThirstValue = Instance.new("IntValue")
	ThirstValue.Name = "Thirst"
	ThirstValue.Parent = NeedsPlayerStats
	
	

	local Hunger
	local Thirst
	local success, err = pcall(function()
		Hunger = NeedsDataService:GetAsync(plr.UserId) or 100
		Thirst = NeedsDataService:GetAsync(plr.UserId) or 100
	end)
	if success then
		HungerValue.Value = Hunger
		ThirstValue.Value = Thirst
	else
		-- Failed to retrieve data
	end

I don’t see any error here at first glance. Can you share a screenshot of the console?

Well, it does not give an error, the data is simply not loaded, the values ​​are kept at 0

1 Like
local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")

--NeedPlayers
local NeedsDataService = datastore:GetDataStore("NeedsData")

players.PlayerAdded:connect(function(plr)
	local Folder = Instance.new("Folder")
	Folder.Name = "DataPlayer"
	Folder.Parent = plr

	local NeedsPlayerStats = Instance.new("Folder")
	NeedsPlayerStats.Name = "NeedsPlayerStats"
	NeedsPlayerStats.Parent = Folder

	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Parent = NeedsPlayerStats

	local Thirst = Instance.new("IntValue")
	Thirst.Name = "Thirst"
	Thirst.Parent = NeedsPlayerStats
	
	

	local HungerGetAsync
	local ThirstGetAsync
	local success, err = pcall(function()
		HungerGetAsync = NeedsDataService:GetAsync(plr.UserId) or 100
		ThirstGetAsync = NeedsDataService:GetAsync(plr.UserId) or 100
	end)
	if success then
		Hunger.Value = HungerGetAsync
		Thirst.Value = ThirstGetAsync
	else
		-- Failed to retrieve data
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local NeedsPlayer = {
		Hunger = plr.DataPlayer.NeedsPlayerStats.Hunger.Value;
		Thirst = plr.DataPlayer.NeedsPlayerStats.Thirst.Value;
	}
	NeedsDataService:SetAsync(plr.UserId, NeedsPlayer)
end)

Here you are trying to set the hunger and the values to the same table datastore.

Hello,

I see in the name of this thread that you also want to know how to save your data to a table which in my opinion is a much better approach, so I’ve fixed the pieces of your code that was not working which were that you were not using the data you got from the table so in the code I have edited for you I use the table which has the values stored in it, the second sort of issue is that you were not using a pcall when saving your data to the data store which could cause errors instead of catching them:

local players = game:GetService("Players")
local datastore = game:GetService("DataStoreService")

--NeedPlayers
local NeedsDataService = datastore:GetDataStore("NeedsData")

players.PlayerAdded:connect(function(plr)
	local Folder = Instance.new("Folder")
	Folder.Name = "DataPlayer"
	Folder.Parent = plr

	local NeedsPlayerStats = Instance.new("Folder")
	NeedsPlayerStats.Name = "NeedsPlayerStats"
	NeedsPlayerStats.Parent = Folder

	local Hunger = Instance.new("IntValue")
	Hunger.Name = "Hunger"
	Hunger.Parent = NeedsPlayerStats

	local Thirst = Instance.new("IntValue")
	Thirst.Name = "Thirst"
	Thirst.Parent = NeedsPlayerStats



	local Needs_Data_Final = nil
	local success, err = pcall(function()
		
		Needs_Data_Final = NeedsDataService:GetAsync(plr.UserId)
		
	end)

	if success and Needs_Data_Final ~= nil then

		Hunger.Value = Needs_Data_Final.Hunger
		Thirst.Value = Needs_Data_Final.Thirst
		
	else
		
		Hunger.Value = 100
		Thirst.Value = 100
	end

end)

players.PlayerRemoving:Connect(function(plr)
	local NeedsPlayer = {
		Hunger = plr.DataPlayer.NeedsPlayerStats.Hunger.Value;
		Thirst = plr.DataPlayer.NeedsPlayerStats.Thirst.Value;
	}
	
	local Success, err = pcall(function()
		
		NeedsDataService:SetAsync(plr.UserId, NeedsPlayer)
		
	end)
	
	if Success then
		
		print("saved")
		
	end
end)

If this code doesn’t work feel free to reply to me!

6 Likes