How could I create multiple tables in a players data using datastore?

for example

local Players = game:GetService("Players")
local DatastoreService = game:GetService("DataStoreService")

local database = DatastoreService:GetDataStore("data")
local PlayerData = DatastoreService:GetDataStore("data", "PlayerData")
local sessionData = {}

local function CachePlayer(Plr: Player)
	local success, errormessage = pcall(function()
		local DataToInsert = {
			["Username"] = Plr.Name
		}
		print(DataToInsert)
		database:UpdateAsync(Plr.UserId, DataToInsert)
	end)
	
end

local function LoadData(Plr: Player)
	local success, Data = pcall(function()
		return database:GetAsync(Plr.UserId)
	end)

	print(Data)
end


Players.PlayerAdded:Connect(CachePlayer)
Players.PlayerAdded:Connect(LoadData)

the players data is a single table; but im trying to get it to be multiple tables
how it is rn

data = {["Username"] = "aeroactual"}

What am triyng to acheive

print(data) = {["Username"] = "aeroactual"},
{["Username"] = "aeroactual}
--etc, a new table created everytime the player joins

i would like it to insert a new table into the players data everytime it joins

so if the player joined once it’d have one table in its data then if it joined twice it’d have 2 tables in its data

right now it replaces/updates the users table
i’d want it so it creates a new table everytime they join and it each one is saved to the users key
is this possible?

2 Likes

Somewhat of a silly question since if you understand datastore you will understand that the data is stored as a table anyway.

You also don’t need to make it a dictionary, you can just make it a table within a table but it is up to you.

example:

local DataToInsert = {
	Username = Plr.Name,
	Second_table = {
		ThirdTable = {
			
		}
	}
}
1 Like

this my first time using Datastore

how could i do this procedurally?
i would like it to insert a new table into the players data everytime it joins

so if the player joined once it’d have one table in its data then if it joined twice it’d have 2 tables in its data

Why? That’s pointless, you will just end up bloating their data for no reason. The larger the data the more time it will take to process all of that data.

1 Like

i know, this is for a specific case and isnt used for a proper game
its only gonna have a couple people join

Then just insert a new tables into the pre-existing table.

How could i do this?
im new to datastore, idk how to have a preexisting one in there

Get user data → after getting insert new table → insert data into new table if no data has been parsed already

1 Like

how would i upload it with the new table?

I am not going to hand hold you. Please refer to the documents. if you do not know how to do something and have the resources to understand please use them.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.