Here is my script.
--Locals
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("MyDataStore")
--Stats adder
game.Players.PlayerAdded:Connect(function(player)
local leaderStats = Instance.new("Folder")
leaderStats.Name = "leaderstats"
leaderStats.Parent = player
--Cash stats
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderStats
-- Load Data
local CashData
local Success, errormessage pcall(function()
CashData = MyDataStore:GetAsync(player.UserId.."-Cash")
end)
if Success then
Cash.Value = CashData
print("Data Loaded")
else
print("Data was NOT loaded :(")
warn(errormessage)
end
end)
--Saveing Stats
game.Players.PlayerRemoving:Connect(function(player)
local Success, errormessage pcall(function()
MyDataStore:SetAsync(player.UserId.."-Cash",player.leaderstats.Cash.Value)
end)
if Success then
print("Data Saved")
else
print("Data was not saved :(")
warn(errormessage)
end
end)

1 comment
What is not working exactly?