Hi. I'm trying to make a data store that saves the amount of cash someone has. But the problem is that it isn't saving. Here is the code.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
function playerJoin(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
--Load Data
local data
local success, errormessage = pcall(function()
local data = myDataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
-- Set our data to the current Money
end
end
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Cash.Value
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data Succesfully Saved!")
else
print("There was an error when saving data!")
warn(errormessage)
end
end)
game.Players.PlayerAdded:Connect(playerJoin)

1 comment
Put print statements to debug it.