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.
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!