Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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 Datastore in Roblox Lua

James

Active Coder
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?
[CODE title="One" highlight="1"]local dataStoreService = game:GetService("DataStoreService")[/CODE]

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.
 

Buy us a coffee!

Back
Top Bottom