Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is it possible to store a value and it's children in a datastore?

To make a long story short, I'm wondering if it's possible to save, specifically, the children of a value (IntValue) in a DataStore. For example, if you have a value called "Money" and it has a child called "Quarter" secondarily added to it, Quarter would become part of the datastore and would save across play sessions. Here is a method I tried to use to do this, although I get an error saying that Arrays can not be saved in datastore.

DS = game:GetService("DataStoreService")
DataStore = DS:GetDataStore("PlayerCurrencies")
local ServerStorage = game:GetService('ServerStorage')

local RS = game:GetService("RunService")



game.Players.PlayerAdded:Connect(function(player)
	local Money = Instance.new('IntValue')
	Money.Name = 'Money'
	Money.Parent = player
	
	local Children = Money:GetChildren()


	local Inventory = Instance.new('Folder', player)
	Inventory.Name = player.UserId

	local data
	local success, err = pcall(function()
		data = DataStore:GetAsync('playerdata_'..player.UserId)
	end)


	if success then
		print('sucess in set async')
		if data then
		
			print('data collected')

			Money.Value = data[1] 
			
			for i, Child in pairs(Children) do
				Child.Parent=Money
			end



		end


		


	end

end)


game.Players.PlayerRemoving:Connect(function(player)
	
	local Money = player.Money
	local Children = Money:GetChildren()
	local ChildTable = {}
	
	for i, Child in pairs(Children) do
		table.insert(ChildTable, Child)
	end

	local data = {Money.Value, ChildTable}

	local success, err = pcall(function()
		DataStore:SetAsync('playerdata_'..player.UserId, data)
	end)

	if success then
		print('data has saved'..Money.Value..ChildTable)
	else
		warn(err)
		print(Children)
		print(ChildTable)
	end
end)


game:BindToClose(function()
	wait(2)
end)

If this is unfixable, is there any alternate methods I could try to use? Thank you.

1 answer

✓ Accepted answer

I'm not sure what player.Money is, but because of you using :GetChildren() I'm going to assume it's a folder.

You can't store Instances, (aka objects), so I think the problems is in:

table.insert(childTable, child)

What I suggest doing, is inserting the child's name, and then having a folder of all possible money, (again, I don't know what the money folder is, so that may be impossible, if so it's time to rewrite your money system.) and when you're retrieving the data, do:

local money = folderOfAllPossibleMoneyObjects:FindFirstChild(childName)
if money then
	money:Clone.Parent = player.Money
end

If you provided more information on what player.Money is, I'd be able to help you further.

This discussion is preserved as it was. New answers, comments, and votes are closed.