DataStores Beginner Video Tututorial

Hey everyone! I recently made a video to help beginner scripters learn how to use DataStoreService. I thought it might be useful for more people if I posted it here.

Finished script:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("DATASTORENAME")

Players.PlayerAdded:Connect(function(Player)
	local Success, PlayerData = pcall(function()
		return DataStore:GetAsync("player_" .. Player.UserId)
	end)
	
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local CoinValue = Instance.new("IntValue")
	CoinValue.Name = "Coins"
	CoinValue.Parent = Leaderstats
	
	if Success then
		CoinValue.Value = PlayerData ~= nil and PlayerData or 0
	end
end)

Players.PlayerRemoving:Connect(function(Player)
	local PlayerCoins = Player.leaderstats.Coins.Value
	
	local Success, Error = pcall(function()
		DataStore:SetAsync("player_" .. Player.UserId, PlayerCoins)
	end)
end)

game:BindToClose(function()
	task.wait(1)
end)

Cheers!

4 Likes

Good tutorial, I would like to see more in tutorial where they tell you why they you have to do things more.
For instance why does :GetAsync error? (I do know this already but beginners wont).
Just things to note for any future tutorials!

2 Likes

That’s a great point! I’ll definitely incorporate more explanations in future tutorials. Thanks for your feedback!

2 Likes