Datastore tables

I’m new to datastores and I don’t know how to store a table using it, how can I make this work?

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("table")
local data
local player_id

	local success, errormessage = pcall(function()
		player_id = player.UserId
		data = datastore:GetAsync(player_id)
	for name, value in pairs do

		local tableee = {
			["Water"] = "Important",
			["Joe"] = "name",
			["Hello"] = "hi"
		datastore:SetAsync(player_id.UserId, tableee)

Error: invalid argument #1 to ‘pairs’ (table expected, got nil)

for i, v in pairs() – there should be a parentheses before the “do”
–which expects a table, whether if it is an array or a dictionary

Also, to that aspect, as you have done

local data
local success, errormessage = pcall(function()
	player_id = player.UserId
	data = datastore:GetAsync(player_id)

it should be something like

if data then
  for name, value in pairs(data) do

if data does exist – i.e. if there is data saved, showing that the player is a player that have played this game before and has data, otherwise, there will be no data (as the player is a new player) and come up with an error

also I have noticed about the

you don’t need to do a separate variable there, just change where you have your playerID variable in the playerAdded function straightaway

just my OCD

Wait sorry, I forgot that you need a parentheses on a for loop. But the thing that I want to know is, how do I edit the things inside the table once I save it as a variable?

What specifically do you mean as a “variable”?

–I am quite confused as to why you don’t have new Instance Values to put inside the player after they have loaded in

Wait sorry, I’m new to datastores and I honestly have no idea how to use it. Can you tell me how I can make a datastore table? And how I can edit the things inside? Sorry if this is a bit confusing, I’m just terrible at explaining things

–reference to roblox datastore api reference

alright, so I will say it in simpler words,

  1. You create Instance values – i.e. boolean values, string values, int values
  2. You attempt to load in data from the data store you have created
  • if successful then you know it is an old player which you would change the value of these instance values to whatever their value is in the datastore
  • if unsuccesful then you know it is a new player which you will then set the default values for new joined players
  1. You will then save the data for players when they leave the game

– an additional comment that I would have towards the way that you set up yours is to not have the player removing event inside the player added event as it doesn’t need to be that way, keep your code cleaner and have less indentations

–Just my OCD

–Let me just quickly type up an example datastore in my roblox studio

Thanks, I’ll appreciate an example

got it done

local datastoreservice = game:GetService("DataStoreService")
local datastore = datastoreservice:GetDataStore("PlayerDatastore")

	local money ="IntValue")
	money.Parent = player
	money.Name = "Money"	
	local gems ="IntValue")
	gems.Parent = player
	gems.Name = "Money"
	local exp ="IntValue")
	exp.Parent = player
	exp.Name = "Money"
	local tableOfData
	local success, errormessage = pcall(function()
		tableOfData = datastore:GetAsync(player.UserId)
	if tableOfData then -- we know that it is a returning player whom already played this game before
		for key, value in pairs(tableOfData) do
			money.Value = tableOfData["Money"]
			gems.Value = tableOfData["Gems"]
			exp.Value = tableOfData["Exp"]
	else -- we know that this player is new or their data got throttled (cached) or whatever you call it and have lost their data
		--sets the default value for new players
		money.Value = 0
		gems.Value = 0
		exp.Value = 0
	local tableOfData = {
			["Money"] = player.Money.Value,
			["Gems"] = player.Gems.Value,
			["Exp"] = player.Exp.Value
	local success, errormessage = pcall(function()
		datastore:SetAsync(player.UserId, tableOfData)

To the extent of typing this long passage up, I would like you to fully understand datastore, or at least don’t feel intimidated by it, so please ask questions on what parts you are confused, as I would need to go to sleep soon :sleeping:

1 Like

Just wondering, do I have to make an int value? If yes, how can I change the value?

Unless you have everything happening on your game in just one script, you would have to have an instance value of some kind

you change it just by changing it’s value

IntValue.Value = IntValue.Value + 100


instance values are used, as they can be read even on local scripts, as well as for the reason that you just need to load values onto new instance values and save their value to your datastore in an orderly fashion

I think I get it now, but just wondering, do I have to make a big change if I use bool values instead?

No, absolutely not, tables doesn’t have to be only integers, they can also integrate strings, boolean values, float (also int values) - decimals, or even table inside tables

local dataTable = {
  ["VIP"] = true,
  ["Money"] = 123,
  ["Gems"] = 100,
  ["LevelInfo"] = {
    ["Level"] = 100,
    ["Exp"] = 10000,
    ["ExpCap"] = 1000000 -- long way to level up lol


I think I get it to some extent now, I really appreciate your help. I’ll message you or respond to one of your replies if I have another question, thanks!