How to make a Quick and Easy Datastore

Datastoring with Kevin! Using Leaderstats!

by Kevinsteo

Author Avatar

What we are trying to Achieve.

Setting Up!

The first thing when making a datastore is to enable API Services such as datastores in the Game Settings and the Security.

image|100x100

After doing that, please make sure to check if you actually done it, next make a ServerScript in ServerScriptService or ServerStorage. I perfer ServerScriptService.

After making the Script type in 3 variables

local Players = game:GetService("Players") -- Players
local DatastoreService = game:GetService("DataStoreService") -- Datastore Service to data store things

local Datastore = DatastoreService:GetDataStore("LuaLearningDatastore") -- Name this to your datastore name!

Now after that, make a LoadData function and SaveData function, and add the player Parameter

Functions

function SaveData(player: Player)

end

function LoadData(player: Player)

end

Players.PlayerAdded:Connect(LoadData) -- Load the Data when they join
Players.PlayerRemoving:Connect(SaveData) -- Save Data when they leave

Simple, now lets do the LoadData first, the hardest :(

function LoadData(player: Player)
	local leaderstats = Instance.new("Folder", player) -- Make a folder and parent it to the player
	leaderstats.Name = "leaderstats" -- Remember to name that correctly!
	
	local cash = Instance.new("NumberValue", leaderstats)
	cash.Name = "Cash" -- Naming it cash cause I want that, name it what you would like!
	
	local level = Instance.new("IntValue", leaderstats) -- Adding more values
	level.Name = "Level"
	
	-- Next add a pcall and connected to the datastore and try to get some data
	
	local success, data = pcall(function()
		return Datastore:GetAsync(player.UserId) -- The Key for the Datastore
	end)
	
	if success then -- If the pcall doesn't error then.
		if data then -- If we fetched some data from the datastore.
			cash.Value = data.cash -- We get the cash data from the Datastore!
			level.Value = data.level
		elseif not data then
			cash.Value = 1 -- The default value
			level.Value = 1 -- The default value for levels
		end
	end
end

Next we can do the SaveData function, it's pretty quick and then it's over!

function SaveData(player: Player)
	local leaderstats = player.leaderstats -- Find the leaderstats
	
	local cash = leaderstats.Cash -- The name you made for the value
	local level = leaderstats.Level -- The name you made for the other value.
	
	-- Make a data Table for the Cash and Level Value.
	
	local Data = {
		cash = cash.Value, -- Cash value and we save the name for the cash, you can change the variable to whatever you had
		level = level.Value, -- Same thing
	}
	
	-- Hook the save function to a pcall so it handles the errors
	
	local success, err = pcall(function()
		Datastore:SetAsync(player.UserId, Data) -- UserId is the key for the datastore so that it can get the data! And then add the data table so that the Load funciton can get the data and adds the values
	end)
end

Overview

This is what the Script is supposed to look like, just incase you forgot anything!

local Players = game:GetService("Players") -- Players
local DatastoreService = game:GetService("DataStoreService") -- Datastore Service to data store things

local Datastore = DatastoreService:GetDataStore("LuaLearningDatastore") -- Name this to your datastore name!

function SaveData(player: Player)
	local leaderstats = player.leaderstats -- Find the leaderstats
	
	local cash = leaderstats.Cash -- The name you made for the value
	local level = leaderstats.Level -- The name you made for the other value.
	
	-- Make a data Table for the Cash and Level Value.
	
	local Data = {
		cash = cash.Value, -- Cash value and we save the name for the cash, you can change the variable to whatever you had
		level = level.Value, -- Same thing
	}
	
	-- Hook the save function to a pcall so it handles the errors
	
	local success, err = pcall(function()
		Datastore:SetAsync(player.UserId, Data) -- UserId is the key for the datastore so that it can get the data! And then add the data table so that the Load funciton can get the data and adds the values
	end)
end

function LoadData(player: Player)
	local leaderstats = Instance.new("Folder", player) -- Make a folder and parent it to the player
	leaderstats.Name = "leaderstats" -- Remember to name that correctly!
	
	local cash = Instance.new("NumberValue", leaderstats)
	cash.Name = "Cash" -- Naming it cash cause I want that, name it what you would like!
	
	local level = Instance.new("IntValue", leaderstats) -- Adding more values
	level.Name = "Level"
	
	-- Next add a pcall and connected to the datastore and try to get some data
	
	local success, data = pcall(function()
		return Datastore:GetAsync(player.UserId) -- The Key for the Datastore
	end)
	
	if success then -- If the pcall doesn't error then.
		if data then -- If we fetched some data from the datastore.
			cash.Value = data.cash -- We get the cash data from the Datastore!
			level.Value = data.level
		elseif not data then
			cash.Value = 1 -- The default value
			level.Value = 1 -- The default value for levels
		end
	end
end

Players.PlayerAdded:Connect(LoadData) -- Load the Data when they join
Players.PlayerRemoving:Connect(SaveData) -- Save Data when they leave

Thanks for reading this whole thing, never knew someone will read this! And donating will support, thanks -- kevin_67070.

View in-game to comment, award, and more!