How to To save Data - Tutorial

dVideo tutorial: How to save data in Roblox Studio - YouTube

Hello, my name is Extreme and I will be teaching you guys how to save data, this can be very essential for your Roblox game as it makes it more interactive.

The First step is to open up a Roblox Studio game, after that you need need to go to settings, by clicking home

home

Now, press game settings

Next, press the security tab

Lastly, turn on API services, this will allow us to save our data


Ok, now we need to add a script into server script service, click the big plus, then click script.

Ok! Now we can get coding! Lets get the data store service and make a data store.

To get the Data Store Service (helps us save data) and to get a data store we type the following code:

local datastoreservice = game:GetService("DataStoreService")
local mydatastore = datastoreservice:GetDataStore("mydatastore")

On the first line, we got a variable which gets the data store service, on the second line, we get a datastore by using the :GetDataStore function, you can change the name of the data store by changing the string inside that function, however be aware that a new data store will mean new data.

Ok, now we can add a playeradded event which will fire when the player joins the game, we can also add a leaderstats folder with an int value to save, the int value will hold our currency.

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"

local coins = Instance.new("IntValue",leaderstats)
coins.Name = "Coins"
coins.Value = 0
end)

Alright, now we need to get the data that will save and set the player’s value to their data,to do this we need to get the player’s UserId and we need to access the data.

local playerid = "Player_" .. player.UserId
local data = mystore:GetAsync(playerid)

We use the UserId because your UserId is not changeable. Your Roblox User can be changeable, so if we were to use the player’s name, then if someone switched their username, your old data would go to them.

Now, we will check if we have data, if we do, we will set the value to the data saved value, however if we have none, we will set our value to 0, which means they are a new player. We will also be saving a table, that is why the value is in table form. The script will go into the data and find the value called “Coins”

if data then
coins.Value = data['Coins']
else
coins.Value = 0
end


Ok, now we actually have to save our data, we just got the data, now we need to save it, to do this, we need to add a player removing event,

game.Players.PlayerRemoving:Connect(function(player)

end)

Now we will add our table, I will name the table data to be saved, inside of the function add

local datatobesaved = {
Coins = player.leaderstats.Coins.Value
}

We also must get the player’s UserId again, we can use the same variable, place this under the table.

local playerid = “Player_” … player.UserId

Now we will add a pcall function, this can check if our data saved or if it did not, we will save our data, and then we can check if it saved or didn’t, to use a pcall we use the function below,

local success, err = pcall(function()
	mystore:SetAsync(playerid,datatobesaved)
end)

The first parameter is the Player’s UserId, the second is the thing(s) we are saving, we placed our value into a table, so thats why we save the whole table.

Finally, we can now check if the data saved or not, to do this we use an if statement,

if success then
		print("data saved!")
	else
		print("data failed to save!")
	end

We use the success part of the data save, if we type if success (which means it saves) then we print in the output “data saved!”, however if it fails to save, we use an else statement and we print in the output “data failed to save!”

Now, we should have, finally, with everything done, this code:

local datastoreservice = game:GetService("DataStoreService")
local mystore = datastoreservice:GetDataStore("mydatastore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	coins.Value = 0

	local gems = Instance.new("IntValue",leaderstats)
	gems.Name

		= "Gems"
	gems.Value = 0


	local playerid = "Player_" .. player.UserId
	local data = mystore:GetAsync(playerid)

	if data then
		coins.Value = data['Coins']
		gems.Value = data['Gems']
	else
		coins.Value = 0
		gems.Value = 0
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local datatobesaved = {
		Coins = player.leaderstats.Coins.Value;
		Gems = player.leaderstats.Gems.Value;

	}
	local playerid = "Player_" .. player.UserId
	local success, err = pcall(function()
		mystore:SetAsync(playerid,datatobesaved)
	end)
	if success then
		print("data saved!")
	else
		print("data faield to save!")
	end
end)

Yeah that is about it, anyways, this was my first tutorial EVER, so if you enjoyed please consider subscrubing to my youtube channel, I make tutorials on stuff like this, here is the channel link, bye! https://www.youtube.com/c/ExtremelyRareNoob

43 Likes

Nice tutorial!

Keep up the good work! :slight_smile:

1 Like

thanks dude, ill make more soon

4 Likes

Maybe you could add at the beginning how to publish your game? Just a simple: hit file and publish, then get a name and description then hit publish again. After this go to home… blah blah blah

That was the only criticism I could get from it, great tutorial!

2 Likes

good idea, but how can i edit posts?

You would hit this button
image

Then edit the post and hit the button you hit to create the post, this will update the post

I would like to provide feedback on your tutorial.

  1. You should also wrap your :GetAsync() in a pcall, as :GetAsync() can also occasionally fail.
  2. Don’t use the second parameter in Instance.new().

This is what you should do to create your objects instead:

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	gems.Parent = leaderstats

	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = leaderstats

Other than that, great tutorial! This will help beginners with saving data for the first time.

3 Likes

“how to to save data”

well, remove one of the “to”

1 Like

Great tutorial, but your syntax does need work.

Usually it doesn’t matter if you are working by yourself (still important!) but if your working on a team or making tutorials, having a consistent style such as using PascalCase or camelCase is important.

Here is a a tutorial I recommend to follow for your next coding tutorial
https://roblox.github.io/lua-style-guide/

Not everything there is needed to be known now, but this is probably where you should start: Roblox Lua Style guide

Why not to use second parameter in Instance.new()?

You should read this.

4 Likes

Thank you, didn’t knew about that!

1 Like

thanks i dont even know what a style is lol thanks

1 Like