I don't know how datastores work AT ALL

  1. What do you want to achieve?
    Creating a DataStore that saves items you’ve bought in the game- for example: In arsenal, if I buy a skin, then that skin saves.

  2. What is the issue?
    I have no idea what I am doing!

  3. What solutions have you tried so far?
    I looked over all the developer hub for answers, and they were either
    A: Too confusing for me, because the OP either already had a good concept of datastores or how they worked
    B: Completely unrelated to what I’m trying to achieve.
    I also tried using ChatGPT to teach me… and I think you can guess how that went :frowning:
    The last thing I did was go to the documentation page for roblox, and I tried to learn DataStores, but either I’m super stupid, or the documentation explains things at a higher level than I can understand.

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!

-- This is all I have so far ):
local ds = game:GetService("DataStoreService")
local mydatastore = ds:GetDataStore("store")

I would like to understand these concepts, and if there is a resource for beginners to learn- THEN PLEASE LIST THEM.

  • Scopes
  • Keys?
  • Basically everything related to whatever pcall and async are.

I’m not asking for anyone to give me a super in-depth, 5 year long college course of a document on how these darn things work, just the basics.

2 Likes

I also want to note- If I come off as a bit pathetic, considering I’ve been coding on Roblox for 7 years, then PLEASE don’t point it out.
Because trust me,

I KNOW.

Roblox provides two really good resources you should read through that might be helpful.

I read through those ): Like I said up here,

1 Like

The two resources I listed will generally give you the best explanation you can get, and any info you would need to know will be there.

However, if you really cannot understand it, I recommend you settle with some youtube tutorials, and when you have a grasp of it, revisit the docs and use what you know to try and understand it better.

In this example, we can think of DataStoreService in figure 1 as all of your experience’s data.

You can get individual “stores,” which can represent various categories of data, and how you categorize it is up to you. They can be thought of as individual parts of your experience’s data, as pictured in figure 2.

Finally, individual key-value pairs can be retrieved. These can be thought of as flash drives, a physically small storage format of data, as seen in figure 3.

A common setup using DataStore service would be the following:

local DataStoreService = game:GetService("DataStoreService") -- all experience data
local Inventories = game:GetDataStore("Inventories") -- all player inventories (items, boosts, whatever)

function LoadPlayer(Player)
    local Inventory = Inventories:GetAsync(Player.UserId) -- this player's inventory

    -- do whatever
end

-- load players on join

Feel free to ask any questions you have. Those pictures are not actual representations of what Roblox’s DataStoreService actually does, in part or in whole. They are only there for visualization purposes.

1 Like

No no, those pictures are definitely accurate representations. (For the sake of learning, we’ll just say that. :wink:)

2 Likes

What I did, was take a leader stats script that had data saving already baked in, then modified it to make a folder called “Upgrades” instead, I added some comments, but I also don’t really know how this stuff works, Just listing a method that helped me figure out what I know so far

Parent script (Inside of serverScriptStorage)

-------------------------------------------------------------------
local Players = game:GetService('Players')
--^ Defines players
local PlayerAdded = function(player)
--Attaches the function	
	local Upgrades = player:FindFirstChild('Upgrades') or Instance.new("Folder", player)
	Upgrades.Name = "Upgrades"
--Creates a folder in the player
	
local UpgradeTest= Upgrades:FindFirstChild('UpgradeTest') or Instance.new("IntValue", Upgrades)
UpgradeTest.Name = "UpgradeTest"
--Creates an Int value in the folder called upgrade test
end

for _, player in next, Players:GetPlayers() do
	spawn(function()
		PlayerAdded(player)
	end)
end
Players.PlayerAdded:Connect(PlayerAdded)

Child script (The thing that actually does the saving)

-- Make sure "Enable Studio Access To API Services" is on in Game Settings! (*OR IT WON'T WORK*) --
local Players = game:GetService("Players")
--Defines players (again)
local DataStoreService = game:GetService("DataStoreService")
local UpgradesSaver = DataStoreService:GetDataStore("Upgrades")
--IMPORTANT - Creates a data store that stores the stuff, in this case i called it upgrades to be consistant with the folder

Players.PlayerAdded:Connect(function(player)
	local UpgradesData = nil
	local success, errormessage = pcall(function()
		UpgradesData = UpgradesSaver:GetAsync(tostring(player.UserId))
	end)

	if success then
		if UpgradesData then
			for i, v in pairs(UpgradesData) do
				player:WaitForChild("Upgrades"):WaitForChild(i).Value = v
			end
		end
	else
		error(errormessage)
	end
end)

--^Some wizzard magic i barely understand, But what it does is it links to that data store, then just sets the value of the players stats that we added in the previous script by matching the names of them (I Think)

local function Save(player)
	local SavedData = {}
	for _, v in pairs(player.Upgrades:GetChildren()) do
		SavedData[v.Name] = v.Value
	end

	local success, errormessage = pcall(function()
		UpgradesSaver:SetAsync(tostring(player.UserId), SavedData)
	end)
	if not success then
		error(errormessage)
	end
end

--No idea how this saves

Players.PlayerRemoving:Connect(Save)

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)

--Save on game close (No idea how this works either)

This actually kinda makes sense now that I look up each term. Thank you!

I’d really recommend not programming your own datastores. There is a lot of complexity that goes into error handling and lots of non-intuitive best practice that you can avoid thinking about by just using an existing datastore solution (Datastore2, ProfileService, etc).

The basic idea behind datastores is that you make a “datastore” to save data:

local mydatastore = ds:GetDataStore("store")

Then you save data to that datastore (which sends a signal from the Roblox server to the Roblox data centers, which can fail sometimes). Then you can get data from the data center by sending another signal.

TL;DR: I’d use an existing datastore solution like DataStore2 or Profile service. There are also some ones that simplify it even further to the point of having no additional code like this

which just saves and loads the leaderstats folder.

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