Storing the position of an object in a datastore

I have a datastore that stores the information about trees in the game. The tables that make up each of these trees looks like this:

local Tree = {
			Type = v.Name,
			Position = {["x"] = Position.X,
				["y"] = Position.Y,
				["z"] = Position.Z
			},
			Items = {
				["Log"] = Nums.Log.Value,
				["Stick"] = Nums.Stick.Value,
				["Sapling"] = Nums.Sapling.Value,
			}
		}

Is there a way to store the position a different way? I’m trying to optimize the store.

Full code if needed:

local DataStoreService  = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local TreeStore = DataStoreService:GetDataStore("WorkspaceTreeStore")

local Update = true 

local function Save(Player)
	local TreeDataStore = {}
	local WorkspaceTrees = workspace.WorkspaceTrees
	for i,v in WorkspaceTrees:GetChildren() do
		local Position = v.PrimaryPart.Position
		local Nums = v:FindFirstChild("Values"):FindFirstChild("Nums")

		local Tree = {
			Type = v.Name,
			Position = {["x"] = Position.X,
				["y"] = Position.Y,
				["z"] = Position.Z
			},
			Items = {
				["Log"] = Nums.Log.Value,
				["Stick"] = Nums.Stick.Value,
				["Sapling"] = Nums.Sapling.Value,
			}
		}

		table.insert(TreeDataStore, Tree)

	end
	warn("Saving Trees")
	local EncodedDataSave = HttpService:JSONEncode(TreeDataStore)
	TreeStore:SetAsync("WorkspaceTreeStore",EncodedDataSave)
end
1 Like

You could save it as a single string with separators between each values.

Here’s an example:

-- Encoding
local Position = Vector3.new(123,456,789)
local DataToStore = tostring(Position) -- "123, 456, 789"

-- Decoding
local DataStored = "123, 456, 789"
local Position = Vector3.new(table.unpack(DataStored:split(","))) -- Vector3.new(123,456,789)

Hope this helps!

1 Like

I tried it and got an error: " attempt to call missing method ‘split’ of table "

Tree:PivotTo(CFrame.new(table.unpack(v.Position:split(","))))
1 Like

It appears you are trying to use the decoding method I provided on data stored using your old encoding method.

1 Like

I used this in the table: Position = {tostring(Position)},

It’s encoded using these 2 lines:

local EncodedDataSave = HttpService:JSONEncode(TreeDataStore)
   TreeStore:SetAsync("WorkspaceTreeStore",EncodedDataSave)

And it’s decoded like this:

local Data = TreeStore:GetAsync("WorkspaceTreeStore")
	local DecodedStore = HttpService:JSONDecode(Data)
1 Like

You don’t need to store it as a table when it only has one entry.
Instead of doing Position = {tostring(Position)}, you should do Position = tostring(Position).

1 Like

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