Help with data stores

I have many questions about data stores. I have read the data store article on dev hub, but I still have a few questions.

First question: If I use set async to set a value for a key, for example, the amount of minutes I have played, and I join a different server, will it have updated for this server or will I have to use update async?

Second question: How could I use a script to update data for a player every 5-10 minutes and when they leave?

Third question: How could I convert this stats script:

local Players = game:GetService("Players")

local function leaderboardSetup(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local guiStats = Instance.new("Folder")
	guiStats.Name = "guiStats"
	guiStats.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Value = 0
	points.Parent = leaderstats
	
	local snowballs = Instance.new("IntValue")
	snowballs.Name = "Snowballs"
	snowballs.Value = 0
	snowballs.Parent = guiStats
	
	local hours = Instance.new("IntValue")
	hours.Name = "Hours"
	hours.Value = 0
	hours.Parent = guiStats
	
	local rank = Instance.new("StringValue")
	rank.Name = "Rank"
	rank.Value = player:GetRoleInGroup(11635716)
	rank.Parent = leaderstats
end

Players.PlayerAdded:Connect(leaderboardSetup)

to use data stores instead of resetting data every time a player joins?

I may have more questions that come up, so I will edit this topic as needed.

1 - I think it does but I’m not sure
2 - Don’t update data every 5 to 10 minutes instead when the player leaves or the server shuts down like this: (you may need to change some things to make it work)

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local dataStore = DataStoreService:GetDataStore("PlayerPoints")--dataStore

Players.PlayerRemoving:Connect(function(plr)
	if RunService:IsStudio() then return end --not update if in studio
	local data = plr.leaderstats.Points.Value --player data
	local sucess,result = pcall(function() --safe call
		dataStore:SetAsync(key..plr.UserId,data)
	end)
	if not sucess then
		warn(result)
	end
	warn(plr.Name.." data saved. Key =  ")
end)

--in case the game is shutted down
game:BindToClose(function()
	if RunService:IsStudio() then return end --not update if in studio
	
	warn("Saving player data.")
	-- go through all players, saving their data
	local players = Players:GetPlayers()
	for _, plr in pairs(players) do
		local userId = plr.UserId
		local data = plr.leaderstats.Points.Value --player data
		local success, result = pcall(function()
			dataStore:SetAsync(userId, data)
		end)
		if not success then
			warn(result)
		end
	end
	warn("Completed saving players data.")
end)

3 - When u create the variable u can create it like this:

local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local dataStore = DataStoreService:GetDataStore("PlayerPoints")--dataStore

Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"

	local PointsValue = Instance.new("NumberValue")
	PointsValue.Parent = folder
	PointsValue.Name = "Points"
	
	--get the value
	local setSuccess, errorMessage = pcall(function()
		PointsValue.Value = dataStore:GetAsync(.plr.UserId) or 0
	end)
	if not setSuccess then --if we can't load data
		warn(errorMessage)
		plr:Kick("Unable to load data, please try later.") --kick the player
	end
end)

The only time that I can think of to use time based saving is for a leaderboard that updates lets say every minute

try putting at the end of the script this makes the player wait 2 seconds in the game before leaving this might solve the saving
plus one question is the data saving once in a while?

game:BindToClose(function()
wait(2)
end)

I want the data to autosave once in a while, in case someone gets disconnected or something, but I don’t know if I need to or not.

You dont need it to save every once in a while just when they leave.

1 Like