How to add more values in datastore by gnomecode

I need help, i have this datastore code by gnomecode but it only supports one instance.
if i wanna add like another value to save it wouldn’t save. how can i modify the script to save?

local players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DatastoreService = game:GetService("DataStoreService")
local database = DatastoreService:GetDataStore("data")
local sessionData = {}

function playerAdded(player)
	local PlayerData = Instance.new("Folder")
	PlayerData.Name = "plrData"

	local FOV = Instance.new("IntValue")
	FOV.Name = "FOV"
	FOV.Parent = PlayerData

	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			return database:GetAsync(player.UserId)
		end)

		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		print("Connected to database")
		if not playerData then
			print("Assigning default data")
			playerData = {
				["FOV"] = 70,
			}
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get data for", player.Name)
		player:Kick("Unable to load your data, try again later.")
	end

	FOV.Value = sessionData[player.UserId].FOV
	FOV.Changed:Connect(function()
		sessionData[player.UserId].FOV = FOV.Value
	end)


	PlayerData.Parent = player
end

players.PlayerAdded:Connect(playerAdded)

function PlayerLeaving(player)
	if sessionData[player.UserId] then
		local success = nil
		local errorMsg = nil
		local attempt = 1

		repeat
			success, errorMsg = pcall(function()
				database:SetAsync(player.UserId, sessionData[player.UserId])
			end)

			attempt += 1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempt == 5

		if success then 
			print("Data saved for", player.Name)
		else
			warn("Unable to save for", player.Name)
		end

	end
end
players.PlayerRemoving:Connect(PlayerLeaving)

function ServerShutdown()
	if RunService:IsStudio() then
		return
	end

	for i, player in ipairs(players:GetPlayers()) do
		task.spawn(function()
			PlayerLeaving(player)
			print("Shutting down, data saving")
		end)
	end
end
game:BindToClose(ServerShutdown)

Add more field to this table to be saved. For eg: if you wanna save a Coins value. You would just do.

playerData = {
	FOV = 70,
    Coins = 100
}

Also, the reason why it wouldn’t save is because when you update the template, all existing data up until that point won’t reflect the change. To combat this, we have a feature called Reconciling which just adds in the missing fields.

I don’t know what reconciling is and have never heard of it

what about the part of

	FOV.Value = sessionData[player.UserId].FOV
	FOV.Changed:Connect(function()
		sessionData[player.UserId].FOV= FOV.Value
	end)

Basically just copies the missing values of template to the target table.

local function ReconcileTable(target, template)
	for k, v in pairs(template) do
		if type(k) == "string" then -- Only string keys will be reconciled
			if target[k] == nil then
				if type(v) == "table" then
					target[k] = DeepCopyTable(v)
				else
					target[k] = v
				end
			elseif type(target[k]) == "table" and type(v) == "table" then
				ReconcileTable(target[k], v)
			end
		end
	end
end

That just sets the value of FOV whenever it changes to the corresponding value in the data table. This is done because you can’t store Instances in datastores.

The instance I wanna add is a boolean value, what would i change about that

You know, you really shouldn’t just copy paste code from tutorials before even learning how it works.

1 Like