How to save the string values inside a folder using data store

alr so basically whenever you join you get a folder thats inside your player object thing and lets say lil old me put some string value into the folder
how do I make it that everything inside the folder saves so when you join next time the same values are in the folder (for example what I sent)

Something like this could be just a number saved.. 1, 2 or 3 in this case. You can create whatever you need off that one simple number save and load.

You can convert the folder into a table. Create a table and loop through every child in the folder.
For example:

local skins = {}
for _, skin in pairs(skinsFolder:GetChildren()) do
table.insert(skins, skin.Value) -- can be also skin.Name. depends on how you read skins.
end

how would I make it so it saves :sweat_smile::sweat_smile::sweat_smile::sweat_smile::sweat_smile::sweat_smile::sweat_smile::sweat_smile::sweat_smile:

Use DataStoreService, there are plenty of tutorials.

Use DataStoreService save every value inside the folder into a table when the player leaves, then load the table and recreate the values when they rejoin.

like this:

local Store = game:GetService("DataStoreService"):GetDataStore("Skins")

game.Players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "Skins"

	for _, data in ipairs(Store:GetAsync(player.UserId) or {}) do
		local value = Instance.new(data.Type, folder)
		value.Name = data.Name
		value.Value = data.Value
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local data = {}

	for _, value in ipairs(player.Skins:GetChildren()) do
		table.insert(data, {
			Name = value.Name,
			Type = value.ClassName,
			Value = value.Value
		})
	end

	Store:SetAsync(player.UserId, data)
end)
1 Like

Use Game:BindToClose to make it more secure

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		local data = {}

		for _, value in ipairs(player.Skins:GetChildren()) do
			table.insert(data, {
				Name = value.Name,
				Type = value.ClassName,
				Value = value.Value
			})
		end

		Store:SetAsync(player.UserId, data)
	end
end)

You do not have to do this It’ll just make your datastore more secure ensuring data save when a server shuts down.

convert it into a dictionary then store it

local Template = {}

for _, dataStoreValue in Player.Skins:GetChildren() do
   Template[dataStoreValue.Name] = dataStoreValue.Value
end

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