Datastore feedback

  1. What I want to achieve is to give this datastore an upgrade. And also is it possible for this datastore to handle 20+ Values for around 15 or 20 player in the same server without having any data failures/losses.

  2. There is no issue yet, I just want to make sure this is good.

  3. No solutions yet.

Down below is the code I currently use.

local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")

local saveDataStore = dataStoreService:GetDataStore("DataStore1")

players.PlayerAdded:Connect(function(plr)

	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	Stats.Parent = plr

	local Points = Instance.new("IntValue")
	Points.Parent = Stats
	while task.wait(.5) do
		Points.Value = math.random(1,1000) -- This is for a test
	end

	local data = saveDataStore:GetAsync(plr.UserId)

	if data then

		for name,value in pairs(data.Stats) do
			
			local success, err = pcall(function()
				Stats[name].Value = value
			end)
		end

	end

end)

players.PlayerRemoving:Connect(function(plr)

	local saveData = {Stats = {}}

	for _,stat in pairs(plr.Stats:GetChildren()) do
		
		local success, err = pcall(function()
			saveData.Stats[stat.Name] = stat.Value
		end)
		
		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end

	end

	saveDataStore:SetAsync(plr.UserId,saveData)

end)

game:BindToClose(function()

	for _,plr in pairs(players:GetPlayers()) do

		local saveData = {Stats = {}}

		for _,stat in pairs(plr.Stats:GetChildren()) do
			
			local success, err = pcall(function()
				saveData.Stats[stat.Name] = stat.Value
			end)
			
			if success then
				print("Data has been saved")
			else
				print("Data has not been saved!")
			end
		end

		wait(7)

	end

end)```
local players = game:GetService("Players")
local dataStoreService = game:GetService("DataStoreService")
local saveDataStore = dataStoreService:GetDataStore("DataStore1")

players.PlayerAdded:Connect(function(plr)
	local Stats = Instance.new("Folder")
	Stats.Name = "Stats"
	Stats.Parent = plr
	local Points = Instance.new("IntValue")
	Points.Parent = Stats
	while task.wait(.5) do
		Points.Value = math.random(1,1000) -- This is for a test
	end
	local data = saveDataStore:GetAsync(plr.UserId)
	if data then
		for name,value in pairs(data.Stats) do
			local success, err = pcall(function()
				Stats[name].Value = value
			end)
		end
	end
end)

players.PlayerRemoving:Connect(function(plr)
	local saveData = {Stats = {}}
	for _,stat in pairs(plr.Stats:GetChildren()) do
		local success, err = pcall(function()
			saveData.Stats[stat.Name] = stat.Value
		end)
		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
	saveDataStore:SetAsync(plr.UserId,saveData)
end)

game:BindToClose(function()
	for _,plr in pairs(players:GetPlayers()) do
		local saveData = {Stats = {}}
		for _,stat in pairs(plr.Stats:GetChildren()) do
			local success, err = pcall(function()
				saveData.Stats[stat.Name] = stat.Value
			end)
			if success then
				print("Data has been saved")
			else
				print("Data has not been saved!")
			end
		end
		wait(7)
	end
end)

Just fixed the formatting, instead of saving a table of values one value at a time for each player you should save the table in its entirety.

I don’t really see any changes tho?

Just fixed the formatting

I was removing the blank lines.