Datastore not saving folder

I have a folder named Inventory which holds BoolValues of item names such as Rocks,Stones,Gold,etc and I want the datastore to save the folder and its contents. Its not saving or displaying any errors.

image

Current datastore code:

    local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')

wait(5)
    game.Players.ChildAdded:Connect(function(player)
    	local data = dss:GetAsync(player.UserId) or {}
    	local inventory = player.Inventory
    	
    	for _,v in pairs(data) do
    		local value = inventory:GetChildren()
    		value.Parent = inventory
    	end
    end)

    game.Players.ChildRemoved:Connect(function(player)
    	local tbl = {}
    	local success, message = pcall(function()

    		for _,v in pairs(player.Inventory:GetChildren()) do
    			tbl[#tbl+1] = v.Name
    		end
    		dss:SetAsync(player.UserId, tbl)
    	end)
    	if success then
    		print("Saved "..player.Name.."'s data")
    	end
    end)
1 Like

Try this script out:

local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')

game.Players.PlayerAdded:Connect(function(player)
	local Success, Error
	Success, Error = pcall(function()
		data = dss:GetAsync(player.UserId)
	end)

	local inventory = player:WaitForChild("Inventory")

	if Success then
		if data ~= nil then
			for i,v in pairs(data) do
				local Item = inventory:FindFirstChild(i)
				Item.Value = v
			end
		end
	else
		print(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local tbl = {}
	for i, v in pairs(player.Inventory:GetChildren()) do
		table.insert(tbl, v.Name, v.Value)
	end
	local success, message = pcall(function()
		dss:SetAsync(player.UserId, tbl)
	end)
	if success then
		print("Saved "..player.Name.."'s data")
	else
		print(message)
	end
end)
1 Like


1 Like

This means that the item that it’s try to find is not in the player’s inventory folder. Try this script to see what is being saved and what is being loaded. Also. you should use the output to see the tables printed.

local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')

game.Players.PlayerAdded:Connect(function(player)
	local Success, Error
	Success, Error = pcall(function()
		data = dss:GetAsync(player.UserId)
	end)

	local inventory = player:WaitForChild("Inventory")

	if Success then
		if data ~= nil then
			print(data)
			for i,v in pairs(data) do
				local Item = inventory:FindFirstChild(i)
				Item.Value = v
			end
		end
	else
		print(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local tbl = {}
	for i, v in pairs(player.Inventory:GetChildren()) do
		table.insert(tbl, v.Name, v.Value)
	end
	print(tbl)
	local success, message = pcall(function()
		dss:SetAsync(player.UserId, tbl)
	end)
	if success then
		print("Saved "..player.Name.."'s data")
	else
		print(message)
	end
end)

How would I define all of the variables in the folder and make it save then? thats what I tried to do with the local Item = inventory:FindFirstChild()

for i, v in pairs(player.Inventory:GetChildren()) do -- This line gets all the inventory's children
	table.insert(tbl, v.Name, v.Value) -- This line puts them into the table along with their values
end

all that code does is create duplicates of the values.
image

1 Like

Did you try my code? What did it print?

1 Like

1 Like

Click the drop down arrow. See what it says.

1 Like

image
image

1 Like

It should say name = value, not number = name. Is it not printing the table at line 29?

No it’s not printing the table. Only in output is the table appearing. the error is the Item.Value = v

image

1 Like

So you’re already creating the BoolValues in the different script when the player joins the game. Ok, didn’t realize that.

What Neon is trying to do is help you save the value of the item to the table so it corresponds with the name of the BoolValue.

However, this would not work.

table.insert(tbl, v.Name, v.Value)

table.insert takes in a number as a second argument, not a key.

Initially you only saved the names of the BoolValues to a table so that’s why I got confused. The only reason someone would do that is if they were only saving a table of strings. There is both an issue with how you save your data and load your data.

Alright I see now. So instead should I make the datastore script create the Inventory folder and all of its values in there.

1 Like

Try this script:

local dss = game:GetService('DataStoreService'):GetDataStore('PlayerData')

game.Players.PlayerAdded:Connect(function(player)
	local Success, Error
	Success, Error = pcall(function()
		data = dss:GetAsync(player.UserId)
	end)

	local inventory = player:WaitForChild("Inventory")

	if Success then
		if data ~= nil then
			print(data)
			for i,v in pairs(data) do
				local Item = inventory:FindFirstChild(i)
				Item.Value = v
			end
		end
	else
		print(Error)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local tbl = {}
	for i, v in pairs(player.Inventory:GetChildren()) do
		tbl[v.Name] = v.Value
	end
	print(tbl)
	local success, message = pcall(function()
		dss:SetAsync(player.UserId, tbl)
	end)
	if success then
		print("Saved "..player.Name.."'s data")
	else
		print(message)
	end
end)
1 Like

image

1 Like

Well that won’t fix your script regardless. I just read Neon’s most recent script, pretty sure it’ll guide you towards the right path.

Wait nevermind that saved the data. image ]

1 Like

Does it load or not? Also, what let me see what the drop down arrow said.

1 Like