Saving object position in SandBox

Hey everyone!

I’m making a sandbox game with plots, and i would like to know how to save the position of each objects.
I already know how to save.
I will save the serialized ID for each object, and i will deserialized when loading saved data.
And then, i’ll save an array like this:

local savedArray = {}
for _, object in pairs(items) do
   if  objectsDictionnary:FindFirstChild(object.Name) then --Check if it is a real object.
      local array = {
         n = serialize(object.Name),
         x = object.Position.X,
         y = object.Position.Y, -- I'm not sure i'll need this value but i'll keep it for now.
         z = object.Position.Z,
         r = object.Orientation.Y
}
      table.insert(savedArray, array)
   end
end

The problem i’m facing is when the system will save the position of each objects, it will save the actual WorldPosition of the objects. So when the player rejoin, if he doesn’t have the same plot, all the saved position will be wrong.

So I’ll need to save the position based on the Grid of the Plot.$

Here is an image to show what I expect to do.

This is my grid, the size of the grid is 4 studs per tile.

I think I have to do something like this:

Something like this.
So when I save the position of an object I have to save the WorldPosition of the object - the position of the tile 0,0. So it should figure like this:

local savedArray = {}
for _, object in pairs(items) do
   if  objectsDictionnary:FindFirstChild(object.Name) then --Check if it is a real object.
      local array = {
         n = serialize(object.Name),
         x = object.Position.X - ZeroTilePosition,
         y = object.Position.Y - ZeroTilePosition, -- I'm not sure i'll need this value but i'll keep it for now.
         z = object.Position.Z - ZeroTilePosition,
         r = object.Orientation.Y
}
      table.insert(savedArray, array)
   end
end

I made this thread because I’m not sure on how to achive this.
Is it the good method? Did I made an error? Does it exist a better way to script this?
Thanks for your help!

1 Like

I would subtract the current plot’s position from the object’s position and save that. When you want to load it back in you would add the current plot’s position to the saved position for each object.

1 Like

Thank you for you help! See you! You helped me a lot.

1 Like

If you need the grid, you can use 2D Arrays, but I guess it is pretty simpler to use ToWorldSpace and ToObjectSpace (orientation will be included).

I made a model if you want to test it out:
Plot.rbxm (3,6,KB)

Code
local model = script.Parent

local plot1 = model.Plot1  -- original plot
local plot2 = model.Plot2  -- plot to load

local block1 = model.Block1  -- original object
local block2 = model.Block2  -- object to load

while true do
	local offset = plot1.CFrame:ToObjectSpace(block1.CFrame)
	block2.CFrame = plot2.CFrame:ToWorldSpace(offset)
	
	wait()
end
1 Like

Thank you i’m testing out what you suggested. I think it can help me a lot! I’ll give you a feedback.

1 Like