Datastore not updating

local function playerRemoved(player)

	local success, data = pcall(function ()
		StatStore:UpdateAsync(player.UserId , function(oldValue)
			return StatService.StatCache[player.UserId]
		end)
		print(StatStore:GetAsync(player.UserId))
	end)

	if not success then
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end
end

Players.PlayerRemoving:Connect(playerRemoved)

no error is outputed, but nothing prints, and the datastore doesn’t change

are you testing that out inside of studio?
Chances are the program exit so fast that it didn’t give it a chance for the script fully run.

Try adding BindToClose, this is the code for when the game suddenly shut down. It gives the game 30 seconds to clean up all the player data, saving, etc.

game:BindToClose(function()
	-- data store stuff here
end)
1 Like

It does not pass the player as an argument, what can i do about that?

You can save it for every player using for i, v loop.

still prints out nothing. :shallow_pan_of_food:

Add print('updated') after the line that prints GetAsync and check again if it prints it or not.

OK, I just realised i didn’t commit changes, but it doesn’t return datastore successfully.

game:BindToClose(function()
	for i , v in pairs(game.Players:GetPlayers()) do
		local player = v

		local success, data = pcall(function ()
			print("FunctionStarted")
		StatStore:UpdateAsync(player.UserId , function()
			return StatService.StatCache[player.UserId]
		end)
		print(StatStore:GetAsync(player.UserId))
	end)

	if not success then
		-- Handle case of error; kick, block saving, etc.
		warn(string.format("Could not load data for %s: %s", player.Name, data))
	end
	end
end)

Try printing the datastore like you do with the UpdateAsync so do

local getSuccess, getData = pcall(function()
	StatStore:GetAsync(player.UserId)
end) 
print(getData) 

Try putting the success stuff inside of the for loop, because the success variable is inside of the for loop scope.

game:BindToClose(function()
	for i , v in pairs(game.Players:GetPlayers()) do
		local player = v

		local success, data = pcall(function ()
			print("FunctionStarted")
		    StatStore:UpdateAsync(player.UserId , function()
			    return StatService.StatCache[player.UserId]
		    end)
		    print(StatStore:GetAsync(player.UserId))
	    end)

        if not success then
		    -- Handle case of error; kick, block saving, etc.
		    warn(string.format("Could not load data for %s: %s", player.Name, data))
	    end
	end
end)

Question: The StatService, is that a custom object you wrote?

It’s a moduleScript :shallow_pan_of_food:

perhaps post the module script code for examination.

I havent added anything beyond a table so far.

So basically, StatService.StatCache[player.UserId] is just a table and no value changes?

Well, I was going to add some functions to change values that are all equal to a value in the datastore.

Okay, let’s check out what the API says and see the sample code.
And perhaps you can simplify the code to have something working and then put them inside of a module.

local success, err = pcall(function()
   pointsDataStore:UpdateAsync(playerKey, function(oldValue)
      local newValue = oldValue or 0
      newValue = newValue + 50
      return newValue
   end)
end)

This is it so far:

local module = {}

module.SetAllStats = function(player , EXPvalue , LVLvalue, PTSvalue)
	local cache = module.StatCache[player.UserId]
	cache.Experience += tonumber(EXPvalue)
	cache.Level += tonumber(LVLvalue)
	cache.Points += tonumber(PTSvalue)
	player.PlayerGui.ScreenGui.ExpFrame.NumberLabel.Text = tostring(cache.Experience)
	player.PlayerGui.ScreenGui.PointsFrame.NumberLabel.Text = tostring(cache.Points)
	player.PlayerGui.ScreenGui.LvlFrame.NumberLabel.Text = tostring(cache.Level)
end

module.StatCache = {}

return module

It seems that you have diverted off track from your original question. :carrot:

Yeah it kind of got solved already