Saving new and previous values in a DataStore

Hello, I am needing help on how to achieve my goal. My goal is to figure out how to save previous values in a DataStore and new values in the same DataStore (Key). Here is what I have in mind so far:

local DataStore = game:GetService("DataStoreService"):GetDataStore("FurniturePositions")

DataStore:SetAsync("HOUSE001", {
   -- someway to include old values here?? (or get old values from this key)
   Lamp = Vector3.new(0,0,0) -- new value
})

Hi, you can get your previous values by using GetAsync and then add your new value in the given table. And then you can SetAsync

should looks like this :

local Lamps = :GetAsync("HOUSE001")
Lamps = table.insert(Lamps, NewValue)
:SetAsync("HOUSE001", Lamps)

I’m not sure if I did what you wanted, but if not tell me.

This isn’t entirely what I’m wanting. Basically everytime a player places down an object (Lamp for example) it will save its position and the name of the object in a DataStore and then whenever a player enters their house it will get all of the objects placed/saved in the building and clone them and position them to the objects saved position (Vector3). Therefore I am needing to add onto the old values incase their is previous objects added and add onto them for new objects in this scenario.

From using table.insert() it would only allow me to save the object name and not the objects position as well.

alr, so maybe you could name the index with your object name and as the value having its position ? with a few furnitures it should looks like this

{
[“x”] = Vector3.new(x,x,x),
[“a”] = Vector3.new(x,x,x),
[“e”] = Vector3.new(x,x,x),
[“g”] = Vector3.new(x,x,x)
{

From using that above this reply I have come up with this:

		if PropertyStore:GetAsync("HOUSE001") then
			local old = PropertyStore:GetAsync("HOUSE001")
			PropertyStore:SetAsync("HOUSE001", {
				table.unpack(old),
				[objectName] = objectPos
			})
		else
			PropertyStore:SetAsync("HOUSE001", {
				[objectName] = objectPos
			})
		end

Unfortunately as well, since it doesn’t allow positions or Vector3.new() I’ll need to convert the objectPos to a string.

wait so how many old positions do you want to save? only one ? like the old value and the new one?

I’m essentially wanting to add-onto the DataStore, so include the old values into the SetAsync(). So it will have the old objects names and positions as well as the new object names and position into the DataStore. So I’m hoping for an output of something like this:

{
["Lamp"] = x,y,z -- old value
["Chair"] = x,y,z -- new value which I just added (somehow)
}

oh okay I thought that you wanted to like save the old position of the object as well as the new one my bad

So if I understood correctly, you should have something like that :

local HouseFurnitures = PropertyStore:GetAsync("HOUSE001") or {}
HouseFurnitures["FurnitureName"] = position --with your position converted into a string tho
PropertyStore:SetAsync("HOUSE001", HouseFurnitures)

To get the output I’m currently doing this:

local PropertyStore = game:GetService('DataStoreService'):GetDataStore("PropertyFurnitureSaveNEWSave")

if PropertyStore:GetAsync("HOUSE001") then
	for i,v in pairs(PropertyStore:GetAsync("HOUSE001")) do
		print(v)
	end
else
	warn("Cannot find Data for this House")
end

But I get an output of the position (I would assume):
image

and what’s the issue here? btw that’s a CFrame so that includes the rotation and also use a variable for PropertyStore:GetAsync(“HOUSE001”) to don’t do useless requests to your datastore, and you should check out profile service bc it’s rlly cool and usefull and makes things easier

The issue is it’s not printing the object name, only it’s CFrame.

I don’t get it, isn’t your cframe printing?

The CFrame is printing but not the object name, I need the object name and the CFrame of the object.

alr then just print i it corresponds to your object name

for i,v in pairs(PropertyStore:GetAsync("HOUSE001")) do
	print(v)
end

or to make things easier to read you can rename them

for objectName, objectCFrame in pairs(PropertyStore:GetAsync("HOUSE001")) do
	print(objectName, objectCFrame )
end

Works!! It gets the position of the object and the object name just as I needed! Thank you ever so much. I’ll go ahead and mark yours as the solution.
image

Great to hear, have a good day :slight_smile:

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