--[[
	3/19/20
	newwil
	DataStore/Leaderstats
--]]


local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("playerData")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	
	local data 
	local success, errormessage = pcall(function()
		data = playerData:GetAsync(player.UserId.."-cash") 
	end)
	
	if success then 
		cash.Value = data
	else
		print("Error while getting your data")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		playerData:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
	end)
	
	if success then
		print("Data successfully saved!")
	else
		print("There was an error while saving the data")
		warn(errormessage)
	end
	
end)
