Saving Multiple String Values in an Already Existing Folder

Then just save the value of the StringValue, as you can save strings to data stores.

local data = {
   StringValue.Value --// If you have multiple values you need to save those inside a table like this
}

local success, result = pcall(function()
   DataStoreService:UpdateAsync(Key, function()
     return data
   end)
end)
2 Likes

so if i wrote

local data = {
	["Money"] = --StringValue.Value
}

It would save in the table?

1 Like

Could you explain “JSON” for me please?

1 Like

Of course! From there, you can reference the value you’ve appended (added) to the dictionary like this:

data["Money"]
print("This user has this much money:", data["Money"])
1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Data = {
			["Primary"] = Character.WeaponSelection.Primary.Value,
			["Secondary"] = Character.WeaponSelection.Secondary.Value,
			["Melee"] = Character.WeaponSelection.Melee.Value
		}
	end)
end)

Is this how i would start making the data storage script?

2 Likes

Yes, although, you would want to create the dictionary when you intend to save the WeaponSelection values to the data store. Creating it now may case trouble, as it won’t keep up to date with any changes done to the WeaponSelection values.

2 Likes

Sorry, I took a two week break to focus on other things, but would I have to create a dictionary or is it not needed in this case.

Also, I want the data storage to save every time the player dies, so when the player dies, the str value would be the same

1 Like

If you’re trying to save multiple pieces of data at once, then yes, you would need to create a dictionary for it. As for saving when the player dies, you can connect your save functions to fire whenever the player’s Humanoid has died.

playerHumanoid.Died:Connect(savefunction)
1 Like
game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Data = {
			["Primary"] = Character.WeaponSelection.Primary.Value,
			["Secondary"] = Character.WeaponSelection.Secondary.Value,
			["Melee"] = Character.WeaponSelection.Melee.Value
		}
	end)
end)

so continuing from this ^^^

How would I save it?
Can you give me an example for the

playerHumanoid.Died:Connect(savefunction)

?

1 Like

I would recommend you to save data using ProfileService out of ease and to prevent data loss. You can find it here: Save your player data with ProfileService! (DataStore Module) - #678 by loleris

The API docs will also provide you with an example of how to save your data, which you can find here: Basic Usage - ProfileService

1 Like

Profile service is kind of hard to understand for beginners. It’s documentation is also confusing. I think suphis data store module is wayyy better and more performant than profile service. Suphis module also provides the same features as profile service such as session locking and many more

Suphi datastore module

1 Like

so how would I use it to save my table?

First of all watch the beginner tutorial video under the thread or just try to understand the post. After you’ve done this, you’ll need to open the players data store then set the date store’s value to your new data. Also, you need to destroy the players datastore when they leave. To get the data you can just use .find to get the players datastore and read its value

This is necessary to save just 3 string values? Is there a simpler way to do it? Because I keep running into errors

I just got online when I saw this but I’ll show you a simplified way of how I would do it.
I didn’t have time to comment on anything so just ask if you’re confused about something

Edit: I noticed I didn’t add loading the data onto the values so I added that. Also, removed unnecessary code.

local Players = game:GetService("Players")

local SDM = require(Path.To.Module)

local Template = {
	Primary = "None",
	Secondary = "None",
	Melee = "None"
}

local function PlayerAdded(Player:Player)
	local PlayerDS = SDM.new("Example", Player.UserId)

	if PlayerDS:Open(Template) ~= "Success" then Player:Kick("Unable to load data!") return end

	local function CreateStringValues(Name:string, Parent:Instance) : StringValue
		local Uhh = Instance.new("StringValue")
		Uhh.Name = Name
		Uhh.Value = PlayerDS.Value[Name]
		Uhh.Parent = Parent

		local Connection:RBXScriptConnection;
		Connection = Uhh.Changed:Connect(function(value: string) 
			if not PlayerDS.State then 
				if Connection then
					Connection:Disconnect()
					Connection = nil
				end
				
				return 
			end

			PlayerDS.Value[Name] = value	
		end)
	end

	local Folder = Instance.new("Folder")
	Folder.Name = "Folder"
	Folder.Parent = Player

	for I, X in pairs(Template) do
		CreateStringValues(I, Folder)
	end
end

local function PlayerRemoving(Player:Player)
	local FoundDS = SDM.find("Example", Player.UserId)
	if FoundDS then FoundDS:Destroy() end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

What is does Uhh represent and can I save it in an already existing folder instead of making a new one?

I didn’t know how to name “Uhh” sorry :frowning:

  1. “Uhh” represents a stringValue.

  2. In the loop you can pass a different parent for the stringValues created. This means you can delete the code that creates a new folder.

  3. Does the code work for you?

Btw wanted to add this… If you change the string value in the folder during run time it will automatically save in the datatstore so you don’t have to worry about manually saving data when a player leaves

Also…

  1. if the variables change in the template outside of the script, it would save
  2. Can I use an existing string value instead of making more?
  1. If you add new data then the next time you load your data table will grow. However, if you switch or delete those three keys, only the next player with no data will get the new updated data template applied as their data table. This means if you wanted to create a fresh new template with different keys then you’d have to create a new datastore or wipe everyone’s data.

  2. Can you show me your original code with your string values? Because the way i did it, the changes to the string values will automatically apply to the keys within the players Datatstore value.

I just basically change the values without data stores

script.Parent.Event.OnServerEvent:Connect(function(player)
	local WeaponTypeValue = player.Character:FindFirstChild("WeaponSelection").Melee 
	local WeaponName = script.Parent.Text

	if WeaponTypeValue.Value ~= WeaponName then 

		if not 	player.Backpack:FindFirstChild(WeaponTypeValue.Value) then
			player.Character:FindFirstChild(WeaponTypeValue.Value):Destroy()
		else
			player.Backpack:FindFirstChild(WeaponTypeValue.Value):Destroy()
		end


		WeaponTypeValue.Value = script.Parent.Text -- <- This changes the value
	end
end)