Saving multiple values to a datastore

How would I save multiple values to my database? Heres my current script:

local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("Inventory")
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local createInv = RS.CreateInventory

players.PlayerAdded:Connect(function(player)
	
	local trails = Instance.new("IntValue")
	trails.Name = "Trails"
	trails.Parent = player
	local equippedTrail = Instance.new("StringValue")
	equippedTrail.Name = "EquippedTrail"
	equippedTrail.Parent = player

	local data
	local success, result = pcall(function()
		data = dataStore:GetAsync("Player_"..player.UserId)
	end)

	if success then
		if data then
			-- I WANT TO ALSO SET THE EQUIPPED TRAIL VALUE HERE
			trails.Value = data
		else
			trails.Value = 4
		end
	else
		warn(result)
	end
	
	createInv:FireClient(player, player.Trails.Value)
	
end)

players.PlayerRemoving:Connect(function(player)	
	local success, result = pcall(function()
		-- AND SAVE THE EQUIPPED TRAIL TOO
		dataStore:SetAsync("Player_"..player.UserId, player.Trails.Value)
	end)

	if success then
		print(result)
	else
		warn(result)
	end
end)

game:BindToClose(function()
	for _, player in ipairs(players:GetPlayers()) do
		local success, result = pcall(function()
			dataStore:SetAsync("Player_"..player.UserId, player.Trails.Value)
		end)

		if success then
			print(result)
		else
			warn(result)
		end
	end
end)

Using tables/dictionaries:

local plr = game:GetService("Players").Localplayer
local sessionData[plr.UserId] = {}

(example)
game:getservice("Players").PlayerRemoving:Connect(function(player: Player)
  local backpack = player:WaitForChild("Backpack")
  if backpack then
    for i, v in pairs(backpack:GetChildren()) do
      sessionData[plr.UserId][i] = v
    end
  end
end)
1 Like
local dataStores = game:GetService("DataStoreService")
local dataStore = dataStores:GetDataStore("Inventory")
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local createInv = RS.CreateInventory

players.PlayerAdded:Connect(function(player)
	local trails = Instance.new("IntValue")
	trails.Name = "Trails"
	trails.Parent = player
	
	local equippedTrail = Instance.new("StringValue")
	equippedTrail.Name = "EquippedTrail"
	equippedTrail.Parent = player

	local data
	local success, result = pcall(function()
		data = dataStore:GetAsync("Player_"..player.UserId)
	end)

	if success then
		if data then
			trails.Value = data.Trails
			equippedTrail.Value = data.EquippedTrail
		else
			trails.Value = 4
		end
	else
		warn(result)
	end

	createInv:FireClient(player, player.Trails.Value)
end)

players.PlayerRemoving:Connect(function(player)	
	local success, result = pcall(function()
		dataStore:SetAsync("Player_" .. player.UserId, {
			Trails = player.Trails.Value;
			EquippedTrail = player.EquippedTrail.Value;
		})
	end)
	
	if not success then
		warn(result)
	end
end)

game:BindToClose(function()
	for _, player in ipairs(players:GetPlayers()) do
		local success, result = pcall(function()
			dataStore:SetAsync("Player_" .. player.UserId, {
				Trails = player.Trails.Value;
				EquippedTrail = player.EquippedTrail.Value;
			})
		end)

		if not success then
			warn(result)
		end
	end
end)
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.