A tad bit stuck tonight. I have been working on making my game save everything and it was going great until I hit this bump here. What I am trying to do is save the contents inside of a folder, and inside the folder is a value called amount.
My first thought was to do something like:
local playeritems = {}
for i,v in pairs(player.PlayerItems:GetChildren()) do
table.insert(playeritems, v.amount.Value…v.Name)
end
Then when I was actually getting the player data, my plan was to just get the first character of the string, and turn this into a number using tonumber. This works great, until my amount is over 10. When the amount is over ten then it is a two digit number and this throws everything off.
Does anyone have an idea on how to do this efficiently? I had another hacky idea and I was going to add a number before the other numbers to show how many character of number there will be proceeding the “tip” number. (I had no idea how to word that sentence haha ) So the save would be like “514245itemsave” then from here:
local digits = string.sub(string, 1,1)
local amount = string.sub(string, 2, tostring(digits))
This would work, but am not exactly sure if this is the best way I could go at it. Let me know, thank you.
If you’re trying to save a folder (or multiple folders) with different value objects you could create a recursing function to basically convert that to an array, and then use HttpService:JSONEncode() to turn the entire array into a string for storage inside of one datastore cell
local HttpService = game:GetService("HttpService")
function Scan()
local function ScanFolder(Folder, Array)
for _,Child in pairs(Folder:GetChildren()) do
if Child:IsA("Folder") then
Array[Child.Name] = {}
ScanFolder(Child,Array[Child.Name])
elseif Child:IsA("ValueBase") then
Array[Child.Name] = Child.Value
end
end
return Array
end
local Array = ScanFolder(game.ServerStorage.Folder,{})
return HttpService:JSONEncode(Array)
end
print(Scan())
So this will grab the entire folder structure and return a string, in my test layout this is what was returned
{"Weapon":{"Ammo":64},"Alive":true,"Money":500}
Which you can save to a datastore, and then use HttpService:JSONDecode() to turn it back into an array and then set up the values again when the player loads.
Yours would have been way more efficient but I got mine to work using my hacky method haha.
if blobsdata:GetAsync(p.userId) then
for i,v in pairs(blobsdata:GetAsync(p.userId)) do
local digits = tonumber(string.sub(v, 1, 1))
local amount1 = string.sub(v, 2, digits + 1)
print(digits)
print(amount1)
print(tostring(string.sub(v, 2 + digits, string.len(v))))
local blob = game.ReplicatedStorage.Blobs:FindFirstChild(tostring(string.sub(v, 2 + digits, string.len(v))))
blob:Clone().Parent = fol
fol:FindFirstChild(tostring(string.sub(v, 2 + digits, string.len(v)))).amount.Value = amount1
end
end
if #player.PlayerBlobs:GetChildren() > 0 then
local playerblobs = {}
for i,v in pairs(player.PlayerBlobs:GetChildren()) do
local firstnum = string.len(tostring(v.amount.Value))
table.insert(playerblobs, firstnum..v.amount.Value..v.Name)
end
blobsdata:SetAsync(player.userId, playerblobs)
end