I want to save these values:
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.