local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Money = Instance.new('NumberValue', leaderstats)
Money.Name = "Money"
Money.Value = 0
local value1Data = Money
local s, e = pcall(function()
value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
end)
if s then
Money.Value = value1Data --setting data if its success
else
game:GetService("TestService"):Error(e) --if not success then we error it to the console
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Money.Value) --setting data
end)
if not s then game:GetService("TestService"):Error(e)
end
end)
local DSService = game:GetService("DataStoreService")
local PlayerStats = DSService:GetDataStore("PlayerStats")
local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Money"
cash.Parent = leaderstats
cash.Value = nil
local key = "User_" .. player.UserId
local res = PlayerStats:GetAsync(key)
if type(res) == "number" then
cash.Value = res
else
cash.Value = 0
end
player.CharacterAdded:Connect(function(character)
local res = PlayerStats:SetAsync(key, cash.Value)
end)
end)
Here you go, I’ve just tested this in studio and it works. When the player joins it either loads their current cash stat from the DataStore (or 0 if they haven’t been stored in the DataStore yet) and then each time the player’s character respawns their cash stat is saved to the DataStore.