How to save these values to datastore?

I want to save these values:
image
These values get inserted from this code:

local function purchaseHero(player, data)
	local HeroesFolder = player:WaitForChild("HeroesFolder")
	local abilityName = HeroData.GetAbilityName(data.Name)
	
	local temp = script:WaitForChild("Template"):Clone()
	temp.Name = data.Name
	temp.Parent = HeroesFolder
	
	for i,v in pairs(abilityName) do
		local abilityTemp = script:WaitForChild("AbilityTemplate"):Clone()
		abilityTemp.Name = v
		abilityTemp.Parent = temp
	end
end

And this is my DataStore:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local SaveInStudio = ServerStorage:WaitForChild("SaveInStudio")

local DataStore = DataStoreService:GetDataStore("Heroes")

local Heroes = workspace:WaitForChild("Heroes")

local function Load(player)
	local PlayerData
	
	local Success, Error = pcall(function()
		PlayerData = DataStore:GetAsync(player.UserId)
	end)

	if not Success then
		player:Kick("Error loading data: " .. Error)
	end

	local HeroesFolder = Instance.new("Folder")
	HeroesFolder.Name = "HeroesFolder"
	HeroesFolder.Parent = player
	
	local PlayerFolder = Instance.new("Folder")
	PlayerFolder.Name = player.Name
	PlayerFolder.Parent = Heroes
	
	if PlayerData then
		for _, Data in ipairs(PlayerData) do
			local temp = script:WaitForChild("Template"):Clone()
			temp.Name = Data.Name
			temp.Equipped.Value = Data.Equipped
			temp.EquippedValue.Value = Data.EquippedValue
			temp.Upgrades.Value = Data.Upgrades
			temp.Parent = HeroesFolder
		end
	end
end

local function Save(player)
	local HeroesFolder = player:WaitForChild("HeroesFolder")
	
	--if not SaveInStudio.Value and game:GetService("RunService"):IsStudio() then return end
	
	if Heroes:FindFirstChild(player.Name) then
		Heroes:FindFirstChild(player.Name):Destroy()
	end

	local PlayerData = {}
	
	for i,v in pairs(HeroesFolder:GetChildren()) do
		local Data = {
			Name = v.Name,
			Equipped = v.Equipped.Value,
			EquippedValue = v.EquippedValue.Value,
			Upgrades = v.Upgrades.Value,
		}
		
		table.insert(PlayerData, Data)
	end
	
	pcall(function()
		DataStore:SetAsync(player.UserId, PlayerData)
	end)
end

Players.PlayerAdded:Connect(Load)

for _, Player in pairs(Players:GetPlayers()) do
	Load(Player)
end

Players.PlayerRemoving:Connect(Save)

I want to be able to save the circled values and set the values back when the player rejoins.

1 Like

Seems like you would just add them to the Data table under the Save function’s for loop.
Something like so:

local Data = {
	Name = v.Name,
	Equipped = v.Equipped.Value,
	EquippedValue = v.EquippedValue.Value,
	Upgrades = v.Upgrades.Value,
	-- New stuff
	Heal = v.Heal.Value,
	Damage = v.Damage.Value
}

Also I love your profile picture lmao

Woops, forgot to actually load them. Moneypro’s solution below should work for that.

if PlayerData then
		for _, Data in ipairs(PlayerData) do
			local temp = script:WaitForChild("Template"):Clone()
			temp.Name = Data["Name"]
			temp.Equipped.Value = Data["Equipped"]
			temp.EquippedValue.Value = Data["EquippedValue"]
			temp.Upgrades.Value = Data["Upgrades"]
			temp.Parent = HeroesFolder
		end
	end
end

local function Save(player)
	local HeroesFolder = player:WaitForChild("HeroesFolder")
	
	--if not SaveInStudio.Value and game:GetService("RunService"):IsStudio() then return end
	
	if Heroes:FindFirstChild(player.Name) then
		Heroes:FindFirstChild(player.Name):Destroy()
	end
1 Like

I don’t want to save it like that because when you purchase a hero, a string value is created and parented in the folder and some heroes won’t have Damage or Heal.