Trouble with Tables in DataStore

Hi, so lately I’ve been working on a new type of DataStore in which I want the DataStore to be able to save extra data such as data stored in tools. So basically, Player > Tools > Tool Data. The tools are saved, but the data stored inside the tool is not.

Here’s the simple idea that I want:

function checkingplayerinfo(player)
local data = datastore:GetAsync(player.UserId)
	
	if data then 
		newdata[player.UserId] = data
		print("Loading Player's Saved Data!")
	else
		data = {
	                        Tools = {[SampleSword1] = {Upgrade = 0,
                                                           MaxUpgrade = 0,
                                                           Stars = 0}, --//Closes the SampleSword1 Table
                                         [SampleSword2] = {Upgrade = 0,
                                                           MaxUpgrade = 0,
                                                           Stars = 0} --//Closes the SampleSword2 Table
                                         }, --//Closes the ToolTable
                                OtherData = {} --//Closes OtherData
				
		} --//Closes the Data Table
		newdata[player.UserId] = data
		print("Giving New Data to New Player!")
	end
	return data
end

Here’s what I have:


function checkingplayerinfo(player)
	
	local data = datastore:GetAsync(player.UserId)
	
	if data then 
		newdata[player.UserId] = data
		print("Loading Player's Saved Data!")
	else
		data = {
				Tools = {},
                                OtherData = {} --//Closes OtherData
		}
		newdata[player.UserId] = data
		print("Giving New Data to New Player!")
	end
	return data
end

--// Calls Data from previous Session
function getplayerdata(player)
	local data = newdata[player.UserId]
	if data then 
		print("Data Received")
		return data
	else
		print("An Error occurred when Getting Data!")
	end
	return false
end
--//Convert Saved Data to Game's DataStore
function setplayerdata(player, data)
	if not data then
		return false
	end
	datastore:SetAsync(player.UserId, data)
	return true
end
--// Saves Data
game.Players.PlayerRemoving:Connect(function(player)
	local settingdata = getplayerdata(player)
	if settingdata then
		for i , v in pairs(settingdata.Tools)do
			table.remove(settingdata.Tools, i) --// Prevents Tool Duplication
		end

 ---------------------- [THIS IS WHERE I'M HAVING TROUBLE!!!] ----------------------
		for i, v in pairs (player:WaitForChild("StarterGear"):GetChildren())do 
			local upgrade = v.Upgrade --// an intvalue stored inside the Weapon
			local maxupgrade = v.MaxUpgrade --// an intvalue stored inside the Weapon
			local stars = v.Stars --// an intvalue stored inside the Weapon

------------------------------------------// I assume this makes the table I want?------------------------------------------
			table.insert(settingdata.Tools, {[v.Name] = {'Upgrade', 'MaxUpgrade', 'Stars'}})
                        settingdata.Tools[v.Name].Upgrade = upgrade.Value
-----------------------------------------attempt to index field '?' (a nil value)-------------------------------------------
                        settingdata.Tools[v.Name].MaxUpgrade = maxupgrade.Value
                        settingdata.Tools[v.Name].Stars = stars.Value
                        print(settingdata.Tools[v.Name].Upgrade) --// Doesn't Work. 
			print("ToolDataSaved")
		end
		setplayerdata(player, settingdata)
		newdata[player.UserId] = nil
		
	else
		print("Error when saving data!")
	end
end)

I really don’t understand how to make a table in another table and grab that custom-named table. That’s my main problem. Any kind of help would be appreciated. Thanks for reading my post! :slight_smile:

It seems you are using .Upgrade and .MaxUpgrade instead of .Upgrades and .MaxUpgrades (plural) when assigning the values to the table in the startergear loop.

Ah that was a mistake by me while making this post. The top side of the argument is just an example of what I wanted it to look like. It doesn’t have anything to do with the actual code after. But I appreciate your feedback, I’ll change it right now on the forum.

instead of using table.insert, since it is probably creating a new table inside the table without key, kind of like: tableName = { { tableStuff here } }
you can simply skip that and just do:

settingsdata.Tools[v.Name] =  {'Upgrade' = 0, 'MaxUpgrade' = 0, 'Stars' = 0}

also you dont need to loop through the table and remove the tools for duplicates, you can just assign: {} and it will clear it for you.

local tableExample = {}
local tableExample["test"] = { "A" = 1, }
print(tableExample.test.A) --> 1
tableExample.test = {} -- removes: A = 1

Yeah I just figured this out a quick minute ago. I was going to result in this way in the end. Thanks for your feedback. Even though I won’t follow exactly this, it’s the exact same layout I planned.

1 Like