DataStore2 Updating a table

I’m using DataStore2 to save tables. I’m having trouble getting it to update a table’s value in my script and am unsure what I am doing wrong. I referred to the API and still can’t get it to work.

In Data Handler:

local defaultPlaceData = {
	PlaceID = 0,--Makes it so by default a new place is made for you
	Version = VersionGui.Text,--Keeps track of the version of the lobby
	Objects = {},--where we save object data
	Floors = 1,--number of floors a player has
	Color = {
		r = (math.random(1,255)),--make players have a random color
		g = (math.random(1,255)),--the first time they join game.
		b = (math.random(1,255))--make sure to divide by 255 when setting colors
		},
	Permissions = {
		OpenToVisitors = 1,--players can join your plot by default
		Managers = {},--players who are deemed worthy to edit owner plot
		Banned = {}--players who are deemed unworthy of visitation
		}
	}

It sets the data to this default table, which appears to work fine.

Then in a script elsewhere:

local BaseIdDataStore = DataStore2("PlaceID", ownerPlayer)
		local ownerData = BaseIdDataStore:Get()
		print(ownerData)
		printDataStore(ownerData)
		if ownerData.PlaceID == 0 then
			print("The player doesn't have a place yet! Let's make one!")
			--hit.Parent.Humanoid:MoveToPart(script.Parent.Parent.Inside)
			local placeName = ownerName.."'s Factory"
			local description = ownerName.."'s Factory created by CreatePlaceAsync function"
			local newPlace = AssetService:CreatePlaceAsync(placeName, place, description)
			if newPlace then
				ownerData.PlaceID = newPlace
				--BaseIdDataStore:Set(ownerData)
				print("NEW:")
				BaseIdDataStore:Set(ownerData)
				printDataStore(ownerData)
			elseif not newPlace then
				print("Error creating new place")
			else print("Catastrophic error, no return")
			end

This is just a part of the script, it narrows down what is important instead of you sifting through the whole thing. This is the same datastore, but after it sets ownerdata, when i call a get later on, it acts like it’s only a number instead of a table.

Our scripts for Datastore2 are different, but I have achieved saving tables fairly simple. Here is the important parts of the script.

local players = game:GetService("Players")

local DS2 = require(game.ServerScriptService.DataStore2)
local MM = require(game.ReplicatedStorage.MathModule)

game.Players.PlayerAdded:Connect(function(player)
	local inv = Instance.new("Folder", player)
	inv.Name = "Inventory"
	
	DS2.Combine("Test", "Inventory")
	
	local invstore = DS2("Inventory", player)

	local invholder = {}
	
	invholder = invstore:Get()
		
	local function SaveInv()
		wait(1)
		invholder = {}
		for i,v in pairs(inv:GetChildren()) do
			local realitem = game.ReplicatedStorage.Items[v.Name]
			local required = require(realitem.Info.Information)
			invholder[tostring(required.Id)] = v.Value
		end
		return invholder
	end
	
	inv.ChildAdded:Connect(function()
		SaveInv()
		wait(.5)
		invstore:Set(invholder)
		for i,v in pairs(inv:GetChildren()) do
			v.Changed:Connect(function()
				SaveInv()
				wait(.5)
				invstore:Set(invholder)
			end)
		end
	end)
	inv.ChildRemoved:Connect(function()
		SaveInv()
		wait(.5)
		invstore:Set(invholder)
	end)

	if invholder == nil then
		local item1 = Instance.new("NumberValue")
		item1.Name = "Stone Mine"
		item1.Value = 3
		item1.Parent = inv
		local item2 = Instance.new("NumberValue")
		item2.Name = "Basic Furnace"
		item2.Value = 1
		item2.Parent = inv
		local item3 = Instance.new("NumberValue")
		item3.Name = "Basic Conveyor"
		item3.Value = 10
		item3.Parent = inv
		print("No existing items were found for "..player.Name..".")
	else
		for i,v in pairs(invholder) do
			for b,a in pairs(game.ReplicatedStorage.Items:GetChildren()) do
				local required = require(game.ReplicatedStorage.Items[a.Name].Info.Information)
				if required.Id == tonumber(i) then
					local item = Instance.new("NumberValue")
					item.Name = a.Name
					item.Value = v
					item.Parent = inv
				end
			end
		end
	end
end)

@YourBoiKdude

I think the part of the script that I’m stuck on is in your modulescript that you’re requiring, could you show that part?

Its a very big module script. Here is the scripts Github Release DataStore2 v1.3.0 · Kampfkarren/Roblox · GitHub

Not datastore2, I’m using that as well…

This is what I mean, what is this doing?

Thats the item’s info, I use it to store stuff like the Item’s ID, Price, Tier, etc. The only thing you would need would be the ID, IF you are using items based on their ID

1 Like

I have no idea why, but it started working suddenly… I think I dove in too deep, thanks for your help kind sir :slight_smile: :grinning:

1 Like