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
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