Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make automatic save stats work?

This is my current module script in server storage:

local PlayerStatManager = {}

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

local sessionData = {}

local AUTOSAVE_INTERVAL = 60

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

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
			sessionData[playerUserId] = data
		else
			sessionData[playerUserId] = {Points=0, Experience=0}
		end
	end
	local data = playerData:GetAsync(playerUserId)
	if data then
		sessionData[playerUserId] = data
		else
			sessionData[playerUserId] = {Points=0, Experience=0}
	    end
	else
		warn("Cannot access data stor for player!")
	end
end

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
		local success, err = pcall(function()
		    playerData:SetAsync(playerUserId, sessionData[playerUserId])
		end)
		if not success then
			warn("Cannot save data for player!")
		end
	end
end)

local function saveOnExit(player)
	local playerUserId = "Player_" .. player.UserId
	savePlayerData(playerUserId)
end

local function autoSave()
	while wait(AUTOSAVE_INTERVAL) do
		for playerUserId, data in pairs(sessionData) do
			savePlayerData(playerUserId)
		end
	end
end

spawn(autoSave)

game.Players.PlayerAdded:Connect(setupPlayerData)

game.Players.PlayerRemoving:Connect(saveOnExit)

return PlayerStatManager

1 comment

0

leaderstats or tools or both because i have a leaderstats script that auto saves

1 answer

local auto_save = coroutine.wrap(function()
	while wait(60) do
		if DSS:GetRequestBudgetForRequestType(Enum.DataStoreRequestType.UpdateAsync) > 0 then
			local success, err = pcall(function()
				for _,player in pairs(game:GetService("Players"):GetPlayers()) do
					local key = player.UserId
					savePlayerData(key)
				end
			end)
		end
	end
end)
	
auto_save()

This will run every 60 seconds. It will check if it can update the values, and if so, it will update them. Otherwise, it will not update, and try again after another 60 seconds. This will run for all players in the game.


--// Edit \--

I configured it to match your script using the savePlayerData function that you have created.

--// Edit \--


Hope this helped!

7 comments

0

Just configure this to match your script, as I don't have any time right now to do so.

0

I just want this to save statistics on leaderstats. Also, is https:JSONEncode a website or something?

0

Alright, I just edited the post for you. https:JSONEncode() is used to encode data into json so it's saved through json tables, and then you use json decode to reset the tables, otherwise you won't be able to set tables again.

0

You'll just have to manually set the values for the player through the savePlayerData function.

0

Thanks! I will give you the acception when my game is finished!

0

Alright

0

Glad to help!

This discussion is preserved as it was. New answers, comments, and votes are closed.