(This only supports leaderstats at the moment.)
WARNING: IF YOU SWITCH FROM THE OLD DATASTORE TO THE NEW ONE, DATA
WILL BE BROKEN, DUE TO THE DATASERVERS HAVING THE SAME DATA NAME.
IT WILL DETECT THAT IT HAS DATA, BUT NOT IN THE FORMAT IT RECOGNISES,
DO NOT SWITCH FROM THE OLD DATASTORE TO THE NEW ONE BEFORE CHANGING THE DATA NAME, AND IT WILL ACCESS THE DEFAULT DATA
Introduction
Making a DataStore can be hard, but this is a simple way to create your own, which can easily support multiple different values and automatically saves them for you!
Why should I use this?
You don’t need to, but this is great for easily creating values, and super easy to customize! The main scripts are located in 2 modules. Build and DataStore. Build has only 1 function which just creates data for you to save, in the format required to save and load data and it will also create the leaderstats for you. DataStore has 4 functions, one of which is not needed to be used, as it is accessed inside the script. An example of making simple leaderstats (does not save) using the Build module:
local Resource = game.ReplicatedStorage["DataStore Resource"]
local reqBuild = require(Resource:WaitForChild("Build"))
local function playerAdded(Player)
local data = reqBuild.BuildNewLeaderstats({
{
Name = "Tickets",
TypeValue = "Int",
Default = 200
}
}, Player)
end
game.Players.PlayerAdded:Connect(playerAdded)
An example of multiple is:
local Resource = game.ReplicatedStorage["DataStore Resource"]
local reqBuild = require(Resource:WaitForChild("Build"))
local function playerAdded(Player)
local data = reqBuild.BuildNewLeaderstats({
{
Name = "Tickets",
TypeValue = "Int",
Default = 200
},
{
Name = "Diamonds",
TypeValue = "Int",
Default = 0
},
}, Player)
end
game.Players.PlayerAdded:Connect(playerAdded)
You need to have the function wrapped in a variable so then you can save the data, like shown in the next section.
DataStore module
This is the main module, the thing that actually… saves the data! To load the data (This will automatically check if the player does not have data) you need to do:
local Resource = game.ReplicatedStorage["DataStore Resource"]
local reqBuild = require(Resource:WaitForChild("Build"))
local reqDataStore = require(Resource:WaitForChild("DataStore")) -- We now need to access the DataStore module
local function playerAdded(Player)
local data = reqBuild.BuildNewLeaderstats({
{
Name = "Tickets",
TypeValue = "Int",
Default = 200
}
}, Player)
data = reqDataStore.LoadData(Player, data) -- Load the data using our new leaderstat data variable
end
game.Players.PlayerAdded:Connect(playerAdded)
But this won’t save our data it will only load it. So lets start saving the data!
local Resource = game.ReplicatedStorage["DataStore Resource"]
local reqBuild = require(Resource:WaitForChild("Build"))
local reqDataStore = require(Resource:WaitForChild("DataStore"))
local function playerAdded(Player)
local data = reqBuild.BuildNewLeaderstats({
{
Name = "Tickets",
TypeValue = "Int",
Default = 200
}
}, Player)
data = reqDataStore.LoadData(Player, data)
end
local function playerRemoving(Player)
reqDataStore.SaveData(Player)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoving)
There we go! We now have a function that will save our data with it! But, what if a player loads in before the PlayerAdded event can fire? Well, just to be safe, what we do is this:
local Resource = game.ReplicatedStorage["DataStore Resource"]
local reqBuild = require(Resource:WaitForChild("Build"))
local reqDataStore = require(Resource:WaitForChild("DataStore"))
local function playerAdded(Player)
local data = reqBuild.BuildNewLeaderstats({
{
Name = "Tickets",
TypeValue = "Int",
Default = 200
}
}, Player)
data = reqDataStore.LoadData(Player, data)
end
local function playerRemoving(Player)
reqDataStore.SaveData(Player)
end
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoving)
for _, plr in pairs(game.Players:GetPlayers()) do
playerAdded(plr)
end
There! Now, the explaining is over, go below to the Resource part if you wish to download it. Thank you for reading!
Resource
So, if you want to use this resource, you can go to
and start using it!
Outroduction
I thank you for reading this, and if you have any questions, want to send feedback or simply have a problem, PM me!