Saving players datas

Hello!
I found this script in roblox hub developper to save players datas but I don’t know how to add a value to Money and Experience and how to get value of experience and money. There are not error in scrpt analysis. Can you help me please?

   -- Set up table to return to any script that requires this module script
    local PlayerStatManager = {}

    local DataStoreService = game:GetService("DataStoreService")
    local playerData = DataStoreService:GetDataStore("PlayerData")

    -- Table to hold player information for the current session
    local sessionData = {}

    local AUTOSAVE_INTERVAL = 60

    -- Function that other scripts can call to change a player's stats
    function PlayerStatManager:ChangeStat(player, statName, value)
    	local playerUserId = "Player_" .. player.UserId
    	assert(typeof(sessionData[playerUserId][statName]) == typeof(value), "ChangeStat error: types do not match")
    	if typeof(sessionData[playerUserId][statName]) == "number" then
    		sessionData[playerUserId][statName] = sessionData[playerUserId][statName] + value
    	else
    		sessionData[playerUserId][statName] = value
    	end
    end

    -- Function to add player to the "sessionData" table
    local function setupPlayerData(player)
    	local playerUserId = "Player_" .. player.UserId
    	local success, data = pcall(function()
    		return playerData:GetAsync(playerUserId)
    	end)
    	if success then
    		if data then
    			-- Data exists for this player
    			sessionData[playerUserId] = data
    		else
    			-- Data store is working, but no current data for this player
    			sessionData[playerUserId] = {Money=0, Experience=0}
    		end
    	else
    		warn("Cannot access data store for player!")
    	end
    end

    -- Function to save player's data
    local function savePlayerData(playerUserId)
    	if sessionData[playerUserId] then
    		local tries = 0	
    		local success
    		repeat
    			tries = tries + 1
    			success = pcall(function()
    				playerData:SetAsync(playerUserId, sessionData[playerUserId])
    			end)
    			if not success then wait(1) end
    		until tries == 3 or success
    		if not success then
    			warn("Cannot save data for player!")
    		end
    	end
    end

    -- Function to save player data on exit
    local function saveOnExit(player)
    	local playerUserId = "Player_" .. player.UserId
    	savePlayerData(playerUserId)
    end


    -- Function to periodically save player data
    local function autoSave()
    	while wait(AUTOSAVE_INTERVAL) do
    		for playerUserId, data in pairs(sessionData) do
    			savePlayerData(playerUserId)
    		end
    	end
    end

    -- Connect "setupPlayerData()" function to "PlayerAdded" event
    game.Players.PlayerAdded:Connect(setupPlayerData)

    -- Connect "saveOnExit()" function to "PlayerRemoving" event
    game.Players.PlayerRemoving:Connect(saveOnExit)

    -- Start running "autoSave()" function in the background
    spawn(autoSave)

    return PlayerStatManager
1 Like

Instead of asking us to help you to implement it, I assume you didn’t learn the basics of DataStoreService. Please learn the basics first.

I read the basics of DataStoreService but it didn’t help me.

I haven’t used the default DataStoreService in a while so I wont be able to help with that, but what I would recommend is DataStore2. In my opinion, it can be very simple if you find the right place to make a basic DataStore2 save.

Check out this topic to learn more about it:
How to use DataStore2 - Data Store caching and data loss prevention - Resources / Community Tutorials - Roblox Developer Forum

I would really recommend learning more about datastore before preceding, in the beginning I didn’t understand them so I just tried to copy code from yt. It didn’t work well, but when I tried a month later once I was a better scripted it all made sense to me. Just keep learning and I would try later on.