How would you datastore a folder with values being added consantly

My problem is that I have a bunch of different items, each of these will be inserted in a folder if won from a case.
image
These values will only be added, how would I use datastore to save all the values in each folder, then insert them again when the player joins?

Keep in mind each player will have a different # of values, because you win them from a case.

1 Like
local itemsOwnedFolder

local saveTable = {}

for _,folder in pairs(itemsOwnedFolder:GetChildren()) do
    saveTable[folder.Name] = {}
    for _,child in pairs(folder:GetChildren()) do
        saveTable[folder.Name][#saveTable[folder.Name]+1] = child.Name
    end
end

This will put all the items into a table to save.

I would save them as tables, and have a different datastore for each set of items maybe. Or you could do a table of tables, e.g. player’s data = {{Items},{Swords},{Titles},{Trails}}

How would that work exactly? I don’t understand what code I need to put in.

see @ineed_massnow 's reply, that should work for your system

Yes but, I don’t understand where to put this in data store

Can you elaborate on what you mean by this? I don’t really understand what you’re asking

Should look something like this

local dss = game:GetService("DataStoreService")
local ds: DataStore = dss:GetDataStore("Your Name Here")

game:GetService("Players").PlayerRemoving:Connect(function(player)

    local saveTable = {}

    local key = player.UserId.."id"

    for _,folder in pairs(player.itemsOwned:GetChildren()) do
        saveTable[folder.Name] = {}
        if (folder.Name == "ItemsEquipped") then
            for _,child in pairs(folder:GetChildren()) do
                saveTable[folder.Name][#saveTable[folder.Name]+1] = child.Value
            end
            continue
        end
        for _,child in pairs(folder:GetChildren()) do
            saveTable[folder.Name][#saveTable[folder.Name]+1] = child.Name
        end
    end

    local s, e = pcall(ds.SetAsync, ds, key, saveTable)

    if (not s) then 
        warn(e)
    end
end)
2 Likes

How would I call upon the data store when a player joins

game:GetService("Players").PlayerAdded:Connect(function(player)

    local key = player.UserId.."id"

    local s, result = pcall(ds.GetAsync, ds, key)

    if (s) then 
        local data = result
        -- do what you want with data
    else
        warn(result)
    end
end)

Thats how you would get the data.

That doesnt work, keep in mind values are not added to a player on spawn, that are added if they win it from a case, how would I put all the values that the player had when they left, into the correct folders, this is my current script, but it doesn’t work.

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Items")

game.Players.PlayerAdded:Connect(function(player)
	local itemsOwned = Instance.new("Folder")
	itemsOwned.Name = "ItemsOwned"
	itemsOwned.Parent = player
	
	local swordsOwned = Instance.new("Folder")
	swordsOwned.Name = "Swords"
	swordsOwned.Parent = itemsOwned
	
	local titlesOwned = Instance.new("Folder")
	titlesOwned.Name = "Titles"
	titlesOwned.Parent = itemsOwned
	
	local trailsOwned = Instance.new("Folder")
	trailsOwned.Name = "Trails"
	trailsOwned.Parent = itemsOwned
	
	local itemsEquipped = Instance.new("Folder")
	itemsEquipped.Name = "ItemsEquipped"
	itemsEquipped.Parent = itemsOwned
	
	local swordEquipped = Instance.new("StringValue")
	swordEquipped.Name = "SwordEquipped"
	swordEquipped.Parent = itemsEquipped
	swordEquipped.Value = "Default"
	
	local trailEquipped = Instance.new("StringValue")
	trailEquipped.Name = "TrailEquipped"
	trailEquipped.Parent = itemsEquipped
	trailEquipped.Value = "None"
	
	local titleEquipped = Instance.new("StringValue")
	titleEquipped.Name = "TitleEquipped"
	titleEquipped.Parent = itemsEquipped
	titleEquipped.Value = "Newbie"
	
	local defaultSword = Instance.new("StringValue")
	defaultSword.Name = "Default"
	defaultSword.Parent = swordsOwned
	defaultSword.Value = "Given"
	local defaultSwordPicture = Instance.new("ImageLabel")
	defaultSwordPicture.Name = "ImageLabel"
	defaultSwordPicture.Parent = defaultSword
	
	local defaultTrail = Instance.new("StringValue")
	defaultTrail.Name = "None"
	defaultTrail.Parent = trailsOwned
	defaultTrail.Value = "Given"
	local defaulttTrailPicture = Instance.new("ImageLabel")
	defaulttTrailPicture.Name = "ImageLabel"
	defaulttTrailPicture.Parent = defaultTrail
	
	local ownerTag = Instance.new("StringValue")
	ownerTag.Value = "Unobtainable"
	ownerTag.Name = "Owner"
	ownerTag.Parent = titlesOwned
	titleEquipped.Value = "Owner"
	local defaultTitlePicture = Instance.new("ImageLabel")
	defaultTitlePicture.Name = "ImageLabel"
	defaultTitlePicture.Parent = ownerTag
	defaultTitlePicture.Image = "rbxassetid://6386451942"
	
	local key = player.UserId.."id"

	local s, result = pcall(dataStore.GetAsync, dataStore, key)

	if (s) then 
		local data = result
		-- do what you want with data
	else
		warn(result)
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)

	local saveTable = {}

	local key = player.UserId.."id"

	for _,folder in pairs(player.ItemsOwned:GetChildren()) do
		saveTable[folder.Name] = {}
		if (folder.Name == "Swords") then
			for _,child in pairs(folder:GetChildren()) do
				saveTable[folder.Name][#saveTable[folder.Name]+1] = child.Value
			end
			continue
		end
		for _,child in pairs(folder:GetChildren()) do
			saveTable[folder.Name][#saveTable[folder.Name]+1] = child.Name
		end
	end

	local s, e = pcall(dataStore.SetAsync, dataStore, key, saveTable)

	if (not s) then 
		warn(e)
	end
end)

something like this

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("Items")

game.Players.PlayerAdded:Connect(function(player)
	local itemsOwned = Instance.new("Folder")
	itemsOwned.Name = "ItemsOwned"
	itemsOwned.Parent = player
	
	local swordsOwned = Instance.new("Folder")
	swordsOwned.Name = "Swords"
	swordsOwned.Parent = itemsOwned
	
	local titlesOwned = Instance.new("Folder")
	titlesOwned.Name = "Titles"
	titlesOwned.Parent = itemsOwned
	
	local trailsOwned = Instance.new("Folder")
	trailsOwned.Name = "Trails"
	trailsOwned.Parent = itemsOwned
	
	local itemsEquipped = Instance.new("Folder")
	itemsEquipped.Name = "ItemsEquipped"
	itemsEquipped.Parent = itemsOwned
	
	local swordEquipped = Instance.new("StringValue")
	swordEquipped.Name = "SwordEquipped"
	swordEquipped.Parent = itemsEquipped
	swordEquipped.Value = "Default"
	
	local trailEquipped = Instance.new("StringValue")
	trailEquipped.Name = "TrailEquipped"
	trailEquipped.Parent = itemsEquipped
	trailEquipped.Value = "None"
	
	local titleEquipped = Instance.new("StringValue")
	titleEquipped.Name = "TitleEquipped"
	titleEquipped.Parent = itemsEquipped
	titleEquipped.Value = "Newbie"
	
	local defaultSword = Instance.new("StringValue")
	defaultSword.Name = "Default"
	defaultSword.Parent = swordsOwned
	defaultSword.Value = "Given"
	local defaultSwordPicture = Instance.new("ImageLabel")
	defaultSwordPicture.Name = "ImageLabel"
	defaultSwordPicture.Parent = defaultSword
	
	local defaultTrail = Instance.new("StringValue")
	defaultTrail.Name = "None"
	defaultTrail.Parent = trailsOwned
	defaultTrail.Value = "Given"
	local defaulttTrailPicture = Instance.new("ImageLabel")
	defaulttTrailPicture.Name = "ImageLabel"
	defaulttTrailPicture.Parent = defaultTrail
	
	local ownerTag = Instance.new("StringValue")
	ownerTag.Value = "Unobtainable"
	ownerTag.Name = "Owner"
	ownerTag.Parent = titlesOwned
	titleEquipped.Value = "Owner"
	local defaultTitlePicture = Instance.new("ImageLabel")
	defaultTitlePicture.Name = "ImageLabel"
	defaultTitlePicture.Parent = ownerTag
	defaultTitlePicture.Image = "rbxassetid://6386451942"
	
	local key = player.UserId.."id"

	local s, result = pcall(dataStore.GetAsync, dataStore, key)

    local function AddItem(folder, itemValue, itemIndex)
        if (folder == itemsEquipped) then -- updates itemEquipped values with the saved ones
            local found = folder:FindFirstChild(itemIndex)
            if (found) then
                found.Value = itemValue
            end
            return
        end
        -- I don't know how you add items. But you would add them here like
        local item = Instance.new("StringValue") -- make the item or clone it
        item.Name = itemValue -- itemValue is the name of the item
        item.Parent = folder -- the folder it was saved from
    end

	if (s) then 
		local data = result
		for _,folder in pairs(itemsOwned:GetChildren()) do
            if (data[folder.Name]) then
                for itemIndex,itemName in pairs(data[folder.Name]) do
                    AddItem(folder, itemName, itemIndex)
                end
            end
        end
	else
		warn(result)
	end
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)

	local saveTable = {}

	local key = player.UserId.."id"

	for _,folder in pairs(player.ItemsOwned:GetChildren()) do
		saveTable[folder.Name] = {} -- Creates a table to store the folders data in

		if (folder.Name == "ItemsEquipped") then -- check if folder is itemsEquipped
			for _,child in pairs(folder:GetChildren()) do
				saveTable[folder.Name][child.Name] = child.Value
			end
			continue
		end

		for _,child in pairs(folder:GetChildren()) do -- Saves all other folders
			saveTable[folder.Name][#saveTable[folder.Name]+1] = child.Name
		end
	end

	local s, e = pcall(dataStore.SetAsync, dataStore, key, saveTable)

	if (not s) then 
		warn(e)
	end
end)

I made a function that gives you the folder the item was in and itemValue/itemName. You don’t have to use itemIndex.

You should learn tables it will help with this.