DataStore Table

Hello,

Im trying to make datastore service save data for the server that will be loaded every few min of a table and a script loops thu the data and gives it to the client Im not too well with datastores

-Glue

What’s the problem? Is there something in the process you don’t understand or are having trouble with?

Getting and saving the table from the server

I dont know how to use datastores that well.

You can use the Datastore:SetAsync() and Datastore:UpdateAsync() methods to change data and Datastore:GetAsync() to retrieve it.

Since you’re unfamiliar with datastores I recommend reading through the wiki page on datastores to first learn about the methods themselves.

Once you’re familiar with the methods you can learn to apply the same principle on tables, most games use a single datastore with tables stored in them, each key-value pair in these tables conveys what value is stored for what data. E.g.

{
    Cash = 100,
    Gems = 700,
    Vehicles = {1, 5, 7, 3}
}

You handle tables as you normally do, the only thing different is you’ll have to accumulate/serialize and deconstruct/deserialize your data between instances and tables .

I hope that’s what you mean sir

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

local DataStore = DataStoreService:GetDataStore("ServerData")

-- Loading our Table.
local function LoadTable(Key)
	local Data
	pcall(function()
		Data = DataStore:GetAsync(Key)
	end)
	
	if Data then
		return Data
	end
end

-- Saving our Table.
local function SaveTable(Key, DataToSave)
	pcall(function()
		DataStore:SetAsync(Key, DataToSave)
	end)
end

-- Usage Example
local ServerData = LoadTable("ServerData")
if ServerData then
	print(ServerData.John.Age)
	print(ServerData.John.Hair)
	print(ServerData.Kyle.Age)
	print(ServerData.Kyle.Hair)
else
	local People = {
		["John"] = {Age = 18, Hair = "Blue"},
		["Kyle"] = {Age = 35, Hair = "White"}
	}
	SaveTable("ServerData", People)
end

I dont userstand on how to because its adding 1 more table every update and with that the server is saving it for a database not a player

So, you want to make a DataStore for the each Player?

I want it to be abled to do a for i,v in pairs(DATABASE) do loop

local function LoadTable(Key)
	local Data
	pcall(function()
		Data = DataStore:GetAsync(Key)
	end)
	
	if Data then
		return Data
	end
end

local function SaveTable(Key, DataToSave)
	pcall(function()
		DataStore:SetAsync(Key, DataToSave)
	end)
end

game.ReplicatedStorage.Client_POST.POST_AddWhoop.OnServerEvent:Connect(function(Table)
	local ServerData = LoadTable("ServerData")
	local NewTemp = TemplateGameData
	
	NewTemp.ID = Table.ID
	table.insert(ServerData, NewTemp)
	
	SaveTable("ServerData", ServerData)
end)

Players.PlayerAdded:Connect(function(Player)
	local Data = LoadTable("ServerData")
	
	for i,v in pairs(Data) do
		local ConfigData = v
		
		v.CreatorName = game:GetService("MarketplaceService"):GetProductInfo(v.ID).Creator.Name
		v.GameName = game:GetService("MarketplaceService"):GetProductInfo(v.ID).Name
		v.GameImage = game:GetService("MarketplaceService"):GetProductInfo(v.ID).IconImageAssetId
		game.ReplicatedStorage.Client_GET.GET_Whoop:FireClient(Player, ConfigData)
	end
end)
``` This is my code

There’s a good handful of problems with this code:

  • GetProductInfo is an asynchronous function that loads information from Roblox. Yielding at that point in your code could be disastrous (ie. cause a race condition)
  • You should NEVER let a client trigger excessive Data Store operations (eg OnServerEvent), as this can allow them to use up a server’s data store budget…which can cause more important things to fail.
  • If you’re allowing the client to ask “can you save this?”, you should be using a RemoteFunction so the server can return something that means “Yes, I saved it” or “No, something went wrong.” With a RemoteEvent, the client can’t be sure the server got the message.

It is not possible to iterate over keys in a data store. Your game must know (or be able to find out) what keys it needs to get, set, update or remove. Data stored within a data store key, however, can be a table which you can iterate over using a typical for-loop, eg for k, v in pairs(t) do ... end