How to make DataStores?

Hi.
I’ve tried reading about DataStores, then trying to make one myself. No luck. It doesn’t work at all. Sometimes it gives an error, and sometimes it doesn’t. I’ve been trying for about a whole day now and it’s extremely annoying. I’ve asked before and not much helped. I always had to go back to square one, either because my entire game broke or nothing worked.

I don’t know what I’m doing wrong.

I want to save these stats and load them when the player joins.

local function onPlayerJoin(player)
   local leaderstats = Instance.new("Folder",player)
   leaderstats.Name = "leaderstats"
   leaderstats.Parent = player
   
   local statistics = Instance.new("Folder",player)
   statistics.Name = "statistics"
   statistics.Parent = player
   
   local moneyStat = Instance.new("IntValue",leaderstats)
   moneyStat.Name = "Money"
   moneyStat.Value = 0
   moneyStat.Parent = leaderstats
   
   local multiplierStat = Instance.new("IntValue",leaderstats)
   multiplierStat.Name = "Multiplier"
   multiplierStat.Value = 1
   multiplierStat.Parent = leaderstats
   
   local maxAmountStat = Instance.new("IntValue",statistics)
   maxAmountStat.Name = "RandomAmountMax"
   maxAmountStat.Value = 5
   maxAmountStat.Parent = statistics
   
   local minAmountStat = Instance.new("IntValue",statistics)
   minAmountStat.Name = "RandomAmountMin"
   minAmountStat.Value = 1
   minAmountStat.Parent = statistics

I’m so close to giving up on learning Lua.

2 Likes

ProfileService is an exceptional module to get started with data stores. There’s a couple of videos linked in the post you can watch.

2 Likes

The videos are already confusing me, it looks too daunting and intimidating and I still have no clue how to add this.

1 Like

I highly suggest you look here: Data Stores | Documentation - Roblox Creator Hub

If you are a person who needs a video, I highly suggest looking here too: https://www.youtube.com/watch?v=DkYupSBUpes

1 Like

I’ve looked at that page and watched that video before and they were both not much help. Still left confused afterwards.

What are you trying to do? Create a datastore?

Yes. A datastore that can save multiple stats. As an example, money and level.

Can you show us the full code. If that’s your full code, it’s obviously wrong, I recommend you to learn the basics of Roblox Lua scripting.

I know the basics. That’s not the full code.

local function onPlayerJoin(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local statistics = Instance.new("Folder",player)
	statistics.Name = "statistics"
	statistics.Parent = player

	local moneyStat = Instance.new("IntValue",leaderstats)
	moneyStat.Name = "Money"
	moneyStat.Value = 0
	moneyStat.Parent = leaderstats

	local multiplierStat = Instance.new("IntValue",leaderstats)
	multiplierStat.Name = "Multiplier"
	multiplierStat.Value = 1
	multiplierStat.Parent = leaderstats

	local rebirthStat = Instance.new("IntValue",leaderstats)
	rebirthStat.Name = "Rebirths"
	rebirthStat.Value = 0
	rebirthStat.Parent = leaderstats

	local maxAmountStat = Instance.new("IntValue",statistics)
	maxAmountStat.Name = "RandomAmountMax"
	maxAmountStat.Value = 5
	maxAmountStat.Parent = statistics

	local minAmountStat = Instance.new("IntValue",statistics)
	minAmountStat.Name = "RandomAmountMin"
	minAmountStat.Value = 1
	minAmountStat.Parent = statistics

	local waitStat = Instance.new("IntValue",statistics)
	waitStat.Name = "waitTime"
	waitStat.Value = 1.3
	waitStat.Parent = statistics
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

Where’s the DataStore variables? Where’s the protective call for GetAsync? I don’t see you’re learning the basics of DataStore, I recommend you just watch a tutorial.

Forgot to add those in. Will do that now.

First, if you haven’t already, enable studio access to API services (Game settings, Security)

What you need to do is create a datastore for each one using DataStoreService (Just going to use your moneyStat as an example) and then tell it to place whatever value that is in the datastore to your moneyStat.

local DataStoreService = game:GetService("DataStoreService")
local moneyDataStore = DataStoreService:GetDataStore("Money")
local function onPlayerJoin(player)
local moneyStat = Instance.new("IntValue",leaderstats)
moneyStat.Name = "Money"
moneyStat.Value = 0
moneyStat.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
	data = moneyDataStore:GetAsync(playerUserId)
end)
if success then
	player.Money.Value = data
	print("Successfully loaded player data!")
else
	print("Error loading in a player's data")
end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

Also please correct me if any of the code is wrong

What if I wanted to save multiple stats?

Then you would just create multiple datastores with each stat that you want:

local moneyDataStore = DataStoreService:GetDataStore("Money")
local statisticsDataStore = DataStoreService:GetDataStore("statistics")
local multiplierDataStore = DataStoreService:GetDataStore("Multiplier")
local maxAmountDataStore = DataStoreService:GetDataStore("RandomAmountMax")
local minAmountDataStore = DataStoreService:GetDataStore("RandomAmountMax")

and then you can modify the pcall function to be:

local data
local data2 -- etc.
local success, errormessage = pcall(function()
	data = moneyDataStore:GetAsync(playerUserId)
    data2 = statisticsDataStore:GetAsync(playerUserId)
    -- Basically just repeat this for each datastore (data3 = (datastore):GetAsync(playerUserId), data4, data5...)
-- Or you can just set up a table of the values like Vong25 said
end)
if success then
	player.Money.Value = data
    player.statistics.Value = data2
   -- You can also just repeat it for each value 
	print("Successfully loaded player data!")
else
	print("Error loading in a player's data")
end
end

But that is just to set the data when the player joins.

Right now the data doesnt get saved when the player leaves so in order to do that:

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local data = player.Money.Value
    local data2 = player.statistics.Value
    -- Pretty much just repeat for each value again
   -- Or you can just set up a table of the values like Vong25 said
	local success, errormessage = pcall(function()
		 moneyDataStore:SetAsync(playerUserId, data)
         statisticsDataStore:SetAsync(player.UserId, data)
            -- and also repeat for each datastore too.
	end)
	if success then
		print("Successfully saved  data")
	else
		print("Error saving data")
	end
end
)

There’s probably a better way than what I am doing.

There is a better way, put all the data in a table and save the table.

local data = {
    Money = 0
    -- rest of data
}
1 Like

There’s just one little problem… that I have no clue how to fix, unless I’m missing something very obvious.
image

It should be r3generat3.leaderstats.Money; you omitted the leaderstats.

Hello! DataStores are an ever growing thing ROBLOX uses to help players save player data in game. Tables are usually the best way to do it!

But, thankfully, there have been methods of DataStores that are easy to use, and simple to operate with! I will provide you with some helpful links to checkout! These links are what helped me, and will help you if you put the time in to read them!

This is a DevForum Post that I found useful!

All of these links should provide you with enough information YOU NEED to start creating your very own DataStore system for tools, money, and more!

Right. Woops. Though, what else happens is that all of my stats get set to 0 by default. Even when I have such things as a Multiplier set to 1 by default.

1 Like

It took me a few months to get datastores working properly. Datastores are tricky because of all the assumptions that tutorials have of your understanding of the API they provide. That’s the reason why so many people opt for a premade datastore handler that is easier to use like DataStore2 or ProfileService which do the tricky stuff for you.

It takes time getting used to it if you are doing it on your own. It might be helpful looking into how DataStore2 and ProfileService implement things (though I have to admit it is confusing to read through).

One thing I noticed in the thread that hasn’t been taken into account is initialized data. If a player has never saved before, their data is nil. You have to check for that, and if you find a nil value, set it equal to the default value. Otherwise you are resetting the default value to nil which I would think has undefined behaviour but apparently it is setting everything to 0 by default for you.