Some data not being saved

Hello! I am having this problem with my data store saving script, obviously there is something wrong, I need help.

--[ SERVICES ]--

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("TimeStats")
local service = game:GetService("MarketplaceService")


--[ LOCALS ]--

local VIPGamepassId = 13913938  -- VIP GAMEPASS ID
local GamepassId = 13914120  -- GAMEPASS ID


--[ FUNCTIONS ]--

game.Players.PlayerAdded:Connect(function(Player)
	

	--[{ LEADERSTATS }]--

    local Leaderstats = Instance.new("Folder")
    Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player
	
	local Minutes = Instance.new("IntValue")
	Minutes.Name = "Minutes" -- changing name here (points, levels, time, etc.)
	Minutes.Value = 0 -- default value
	Minutes.Parent = Leaderstats
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash" -- changing name here (points, levels, time, etc.)
	Cash.Value = 0 -- default value
	Cash.Parent = Player
	
	
	--[{ DATA STORE }]--
	
	
	local Data = DataStore:GetAsync(Player.UserId) -- Get Data
	
	if type(Data) ~= "table" then
		Data = nil
	end
	
	if Data then
		Minutes.Value = Data.Minutes
		Cash.Value = Data.Cash
	end
	
	local incrementValue = 1 -- value when adding points

	if (service:UserOwnsGamePassAsync(Player.UserId, VIPGamepassId)) then -- 3x gamepass
		incrementValue = 3
	elseif (service:UserOwnsGamePassAsync(Player.UserId, GamepassId)) then -- 2x gamepass
		incrementValue = 2
	end
	

	--[{ TIME GIVERS }]--
	
	
	coroutine.resume(coroutine.create(function() -- gives 1 point every minute
		while true do
			wait(60) -- every minute
			Minutes.Value = Minutes.Value + incrementValue --  adds points based off of the incrementValue
		end
	end))
	
	coroutine.resume(coroutine.create(function() -- gives 1 point every minute
		while true do
			wait(30) -- every 30 seconds
			Cash.Value = Cash.Value + incrementValue -- adds cash
		end
	end))
	
	
end)




game.Players.PlayerRemoving:Connect(function(Player) -- save function here
	
	--[{ DATA STORE SAVING }]--
	
	DataStore:SetAsync(Player.UserId, { -- gets data
		Minutes = Player.leaderstats.Minutes.Value,
		Cash = Player.Cash.Value
	})
	
end)

It’s not as simple as that, this line can error and that’s what protected call, or pcall, are for. You can probably read more about it here:

However, I am still not able to pinpoint what the issue is. Can you please elaborate the problem?

So, Basically when I Play, right, it works for the next 2 times I play, then all of a sudden it just resets like there is no tomorrow.

Maybe change this to
Minutes.Value = DataStore:GetAsync(Player.UserId) or 0
Not really sure what your problem is.

Maybe it’s better to just separate each Datastore, one for minutes and one for cash.

NEVER separate your DataStores for individual values. OP is correct to be saving their data as a table. Dedicating a DataStore or key to a single player data value is a bad practice that spawned when DataStores were first introduced and it’s a great way to trip limits and cause further issues.

The issue is unclear because it’s just code dumped into the thread asking for help. OP needs to elaborate clearly about what the problem is and do some basic debugging (e.g. prints, checking the console and the like) so we have more information to work from.

1 Like

Does anything print out in the output