What did I do wrong here? First time saving data with tables

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to save data using tables.
  2. What is the issue? Include screenshots / videos if possible!
    image
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    The one below is the only one I tried.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Hey, I’ve tried implementing a solution (of course I did it on a workbench to not mess up my main game), and I’m getting this error.

This is my updated code (this is a hacky solution, I’m trying to get it done quick for now).

local function saveData(player)
	local saveTable = {}
	saveTable.values = {}
	saveTable.values.money = player.money.Value
	saveTable.values.days = player.days.Value
	saveTable.values.daysWait = player.daysWait.Value
	
	testDataStore:SetAsync(player.UserId, saveTable)
end

local function loadData(player, choice)
	local saveTable = {}
	
	saveTable.values = {}
	
	if choice == 1 then
		saveTable.values.money = testDataStore:GetAsync(player.UserId, saveTable.values.money)
	elseif choice == 2 then
		saveTable.values.days = testDataStore:GetAsync(player.UserId, saveTable.values.days)
	elseif choice == 3 then
		saveTable.values.daysWait = testDataStore:GetAsync(player.UserId, saveTable.values.daysWait)
	end
	
	testDataStore:GetAsync(player.UserId, saveTable)
end

and this is my code for getting the data:

game.Players.PlayerAdded:Connect(function(plr)
	 local player = plr
	 
	 local money = Instance.new("IntValue")
	 local days = Instance.new("IntValue")
	
	 money.Parent = plr
	 days.Parent = plr
	
	 money.Name = "money"
	 days.Name = "days"
	
	 money.Value = testDataStore:GetAsync(loadData(plr, 1)) or 0
	 days.Value = testDataStore:GetAsync(loadData(plr, 2)) or 0
	 
	 saveData(plr)
end)

What did I do wrong here?

First of all, Your loadData function does not return anything and You use :GetAsync() method way too much. You need to call this method only once and wrap in pcall() to handle errors.

local function loadData(player)
	local saveTable = {}
	
	local success,err = pcall(function()
		saveTable = testDataStore:GetAsync(player.UserId) --GetAsync() only needs a Key and it will return the whole saved table
	end)
	
	if success then
		return saveTable
	else
		return err
	end
end

game.Players.PlayerAdded:Connect(function(plr) 
	 local data = loadData(plr) --If GetAsync() was done without errors this will return your saveTable
	 local money = Instance.new("IntValue")
	 local days = Instance.new("IntValue")
	
	 money.Parent = plr
	 days.Parent = plr
	
	 money.Name = "money"
	 days.Name = "days"
	 if data and data.values then 
	 	money.Value = data.values.money 
	 	days.Value = data.values.days
	 elseif data == nil then
		--A new player joined the game
        saveData(player)
     else
        --There was a error.
        error(data)
	 end
end)
2 Likes

Thank you for explaining it! Everything is working as intended now

1 Like