I am making a game where users place different bricks and I want the position of all of their bricks to be saved through DataStores....having to spawn an IntValue for every brick is quite tedious. Is there a way I can just loop through all of the bricks theyve placed, get the position value and store it in a table for the DataStore or something?
COMMUNITY QUESTION
Is there a way to save datastore values besides using IntValues?
1 answer
You don't have to use IntValues. You can store numbers, strings, arrays, and dictionaries in DataStores directly. You can store the coordinates as a dictionary (say, {x = 1, y = 4.5, z = 9} and upload that dictionary into a DataStore.
EDIT: Since you asked, here's how I would do it.
local DataStoreService = game:GetService("DataStoreService")
local modelStore = DataStoreService:GetDataStore("ModelStore")
-- Encode the part as a table and
-- return a dictionary.
function SerializePart(part)
local serializedPart = {}
serializedPart.Position.x = part.Position.x
serializedPart.Position.y = part.Position.y
serializedPart.Position.z = part.Position.z
return serializedPart
end
-- Decode the table generated from SerializePart
-- and create and return a part out of it.
function DeserializePart(serializedPart)
local part = Instance.new("Part")
part.CFrame = CFrame.new(
serializedPart.Position.x,
serializedPart.Position.y,
serializedPart.Position.z,
)
return part
end
-- Encode each part of the model and
-- return it as an array of serializedParts.
function SerializePartModel(model)
local serializedModel = {}
for _, part in pairs(model)
if part:IsA("Part") then
local serializedPart = SerializePart(part)
table.insert(serializedModel, serializedPart)
end
end
return serializedModel
end
-- Decode each part in the array 'serializedModel'
-- and return a model from it.
function DeserializePartModel(serializedModel)
local model = Instance.new("Model")
for _, serializedPart in pairs(serializedModel) do
local part = DeserializePart(serializedPart)
part.Parent = model
end
return model
end
local testModel = game.Workspace.Model
local serializedModel = SerializePartModel(testModel)
modelStore:SetAsync("testModel", serializedModel)
-- later, when we want to retreive testModel
local DataStoreService = game:GetService("DataStoreService")
local modelStore = DataStoreService:GetDataStore("ModelStore")
local serializedModel = modelStore:GetAsync("testModel")
local testModel = DeserializePartModel(serializedModel)
testModel.Parent = workspace
This will only store the Position of the part, not its Size, CFrame, Color, etc. I have not tested this, but this should work. Maybe.
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Any way I can get some hand holding? I only know how to use DataStore with saving IntValues, any recommended videos or anything?