How do you save the value of a string value inside of a table with datastore2?

i need to save the value of a stringvalue if that makes sense. right now im saving values inside of a table and then making new stringvalues when the player joins and add them into a folder but the values will be blank how do i save the value and then load it into the string value? with Datastore2 btw

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DataStore2 = require(ReplicatedStorage:WaitForChild("DataStore2"))

DataStore2.Combine("MasterKey","PlayerDatastore")

local DefaultData = {
	Coins = 0,
	Exp = 0,
	Level = 1,
	SkillPoints = 0,
	Health = 100,
	Physical = 0,
	Spell = 0,
	EquippedWeapon = "None",
	EquippedArmor = "None",
	EquippedHelmet = "None",
	EquippedSpell1 = "None",
	EquippedSpell2 = "None",
	OwnedItems = {},
}

game.Players.PlayerAdded:Connect(function(player)
	--local character = player.Character or player.CharacterAdded:Wait()
	
	local PlayerDatastore = DataStore2("PlayerDatastore", player)
	
	local Data = PlayerDatastore:Get(DefaultData)
	
	local DataFolder = script.DataFolder:Clone()
	DataFolder.Parent = player
	
	DataFolder.Coins.Value = Data.Coins
	DataFolder.Exp.Value = Data.Exp
	DataFolder.Level.Value = Data.Level
	DataFolder.SkillPoints.Value = Data.SkillPoints
	DataFolder.Health.Value = Data.Health
	DataFolder.Physical.Value = Data.Physical
	DataFolder.Spell.Value = Data.Spell
	DataFolder.EquippedArmor.Value = Data.EquippedArmor
	DataFolder.EquippedHelmet.Value = Data.EquippedHelmet
	DataFolder.EquippedSpell1.Value = Data.EquippedSpell1
	DataFolder.EquippedSpell2.Value = Data.EquippedSpell2
	DataFolder.EquippedWeapon.Value = Data.EquippedWeapon
	
	for i, item in pairs(Data.OwnedItems) do

		local ItemV = Instance.new("StringValue")
		ItemV.Name = item
		ItemV.Value = "Legendary" --need this value to save and load whatever it is (common, legendary, rare etc..)
		ItemV.Parent = player.DataFolder.OwnedItems
	end
end)

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

	local Data = {}
	local OwnedItems = {}
	local PlayerDatastore = DataStore2("PlayerDatastore", player)
	
	for i,v in pairs(player.DataFolder:GetChildren()) do
		if v.ClassName ~= "Folder" then
			Data[v.Name] = v.Value
		elseif v.ClassName == "Folder" and v.Name == "OwnedItems" then
			for i, item in pairs(v:GetChildren()) do
				table.insert(OwnedItems, item.Name)
			end
		end
	end
	 
	Data["OwnedItems"] = OwnedItems
	PlayerDatastore:Set(Data)
	PlayerDatastore:Save()
end)


do you have script you can provide?

LOL yeah i should have probably added that into the post

maybe try saving each item as a table with its name and value?
and when loading the data you create a string with the name and value from the table?

something like this:
player removing function to save

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

    local Data = {}
    local OwnedItems = {}
    local PlayerDatastore = DataStore2("PlayerDatastore", player)

    for i,v in pairs(player.DataFolder:GetChildren()) do
        if v.ClassName ~= "Folder" then
            Data[v.Name] = v.Value
        elseif v.ClassName == "Folder" and v.Name == "OwnedItems" then
            for i, item in pairs(v:GetChildren()) do
                -- Save each item as a table containing the name and value
                table.insert(OwnedItems, {Name = item.Name, Value = item.Value})
            end
        end
    end

    Data["OwnedItems"] = OwnedItems
    PlayerDatastore:Set(Data)
    PlayerDatastore:Save()
end)

example of how to load the data

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

    local PlayerDatastore = DataStore2("PlayerDatastore", player)

    local Data = PlayerDatastore:Get(DefaultData)

    local DataFolder = script.DataFolder:Clone()
    DataFolder.Parent = player

    --clear space for more loading stuff

    for i, itemTable in pairs(Data.OwnedItems) do
        local ItemV = Instance.new("StringValue")
        ItemV.Name = itemTable.Name
        ItemV.Value = itemTable.Value -- Load the item value from the saved data
        ItemV.Parent = player.DataFolder.OwnedItems
    end
end)

(may not work since i don’t quite remember how datastore2 works)

1 Like

this worked just how i needed it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.