I want to save different “charms”,like folders, and the values in them, like upgrades to make your character stronger
i am kinda new to scripting and the biggest issue for me is saving data.
local CHarms = game:GetService("ReplicatedStorage"):WaitForChild("Charms")
local players = game:GetService("Players")
local data = game:GetService("DataStoreService"):GetDataStore("Charms")
players.PlayerAdded:Connect(function(plr)
local charms = Instance.new("Folder",plr)
charms.Name = "Charms"
local owendC = plr:WaitForChild("Charms")
local charmsowned = {}
pcall(function()
charmsowned = data:GetAsync(plr.UserId) or {}
end)
for i,own in pairs(charmsowned) do
if CHarms:FindFirstChild(own) then
local clone = CHarms[own]:Clone()
clone.Parent = owendC
else
owendC = charmsowned
end
end
end)
function plrLeaving(plr)
local ownedcharms = {}
for i , charm in pairs(plr.Charms:GetChildren()) do
table.insert(ownedcharms,charm.Name)
for _ , child in pairs(charm:GetChildren()) do
table.insert(ownedcharms , child.Value)
--ownedcharms[charm.Name][child.Name] = child.Value
--TableToSave[value.Name] = {[child.Name] = child.Value}
end
end
local s,e = pcall(function()
data:SetAsync(plr.UserId,ownedcharms)
end)
if s then
print(s)
else
print(e)
end
end
Try this, but I am not sure if it is what you are looking for.
local CHarms = game:GetService("ReplicatedStorage"):WaitForChild("Charms")
local players = game:GetService("Players")
local data = game:GetService("DataStoreService"):GetDataStore("Charms")
players.PlayerAdded:Connect(function(plr)
local charms = Instance.new("Folder",plr)
charms.Name = "Charms"
local owendC = plr:WaitForChild("Charms")
local charmsowned = {}
pcall(function()
charmsowned = data:GetAsync(plr.UserId) or {}
end)
for i,own in pairs(charmsowned) do
if CHarms:FindFirstChild(i) then
local clone = CHarms[i]:Clone()
clone.Parent = owendC
else
owendC = charmsowned
end
end
end)
function plrLeaving(plr)
local ownedcharms = {}
for i , charm in pairs(plr.Charms:GetChildren()) do
local tbl = {}
for _ , child in pairs(charm:GetChildren()) do
tbl[child.Name] = child.Value
end
ownedcharms[charm.Name] = tbl
end
local s,e = pcall(function()
data:SetAsync(plr.UserId,ownedcharms)
end)
if s then
print(s)
else
print(e)
end
end
Try setting the values of the new cloned folder, maby they are already stored. Someything like this:
players.PlayerAdded:Connect(function(plr)
local charms = Instance.new("Folder",plr)
charms.Name = "Charms"
local owendC = plr:WaitForChild("Charms")
local charmsowned = {}
pcall(function()
charmsowned = data:GetAsync(plr.UserId) or {}
end)
for i,own in pairs(charmsowned) do
if CHarms:FindFirstChild(i) then
local clone = CHarms[i]:Clone()
for val,v in pairs(own) do
if clone:FindFirstChild(val) then
clone[val].Value = v
end
end
clone.Parent = owendC
else
owendC = charmsowned
end
end
end)
hii is there any way i can also save the name of the value? since i already use the name as the value. but i need to change the name of the value mid game