• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.
    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Lua Tutorial Datastore in Roblox Lua

James

New Coder
Apr 8, 2019
29
15
4
NOTE: THIS TUTORIAL IS FOR ROBLOX LUA ONLY, NOT ANY OTHER TYPE OF LUA.

What is datastore?

datastore is a Roblox service used for saving data, in my opinion, it is very easy but I know some people have some trouble with it.

This tutorial is going to go over the very basics.

How do I use datastore?
One:
local dataStoreService = game:GetService("DataStoreService")
This code gets the datastore service
With this service, you can do the data saving.

How do I save?
So, to save you need to call the function SetAsync
SetAsync takes 2 arguments, Key and Data

The Key value is the key associated with the data you are saving, this is useful for getting that data after it has been saved.

The data value is what you are saving, this can be anything from a number to a dictionary

Code:
local dataStoreService = game:GetService("DataStoreService")

dataStoreService:SetAsync("ThisIsTheKey","ThisIsTheData")

dataStoreService:SetAsync("ThisIsAnotherKey,5)
Okay, now how do I get that saved data?
To get data you have saved is very easy

You use a function called GetAsync

GetAsync takes one variable, the Key

The Key is what you saved the data with

Code:
local dataStoreService = game:GetService("DataStoreService")

local Data = dataStoreService:GetAsync("ThisIsTheKey")
In the event that no data is saved to the key, you give the function returns a nil value

You can validate data by doing
local Data = dataStoreService:GetAsync("ThisIsTheKey") or 5

This makes it so that it either gets the key to that data or returns 5

Alright, that is it for basic datastore, if you want to see how to put this into action and make a leaderstats saving system then like this post and I will do one!
Message me at JamesWa#2406 on discord if you need help.