Hello ive been wanting to do a project but ive ran into a problem: data stores, i have no idea how they work and how to save instances like models into them, also i dont want to save models whit each one of its properties into a datastore that would be so inneficient, could someone help me?
Hi, you can look up on youtube or elsewhere how to use datastores, or you could use datastore2 or profilestore. (with profilestore being more secure) I recommend watching the tutorial video for profilestore.
Idk if the objects will be placed relative to another part, like a plane or some kind of surface the models are placed on. But if its not relative to another part you can change relativePosition in just the model.PrimaryPart.Position. The parts cframe will have to be serialized into a string to save them. And we want to deserialize them again in a cframe.
These are serialize functions:
local function SerializeCFrame(cf)
local a, b, c, d, e, f, g, h, i, j, k, l = cf:GetComponents()
return string.format("%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f",
a, b, c, d, e, f, g, h, i, j, k, l)
end
local function DeserializeCFrame(str)
local components = {}
for value in string.gmatch(str, "[^,]+") do
table.insert(components, value)
end
return CFrame.new(
components[1], components[2], components[3],
components[4], components[5], components[6],
components[7], components[8], components[9],
components[10], components[11], components[12]
)
end
For the datastore you will have to have an index in the player data with savedObjects or something. In this table we will save the objects.
local plane = -- plane or surface
local objects = objectFolder:GetChildren() -- get all object out of a folder
local objectData = {}
for i, model in pairs(objects) do
local relativePosition = plane.CFrame:PointToObjectSpace(model.PrimaryPart.Position)
local objectID = model.Name
local cframe = SerializeCFrame(CFrame.new(relativePosition) * model.PrimaryPart.CFrame.Rotation)
-- CHOOSE WHICH ONE WORKS BEST FOR YOU
--objectData[cframe] = objectID -- cframe should be unique or other models get overwritten
--table.insert(objectData, {objectID = objectID, cframe = cframe}) -- saves all objects no matter what
end
-- path to save data to. index should be a table, and index2 should be a unique value to not override any model
-- use for example the cframe for this
playerData[INDEX][INDEX2] = objectData
I personally use profilestore but if youd like to use something else and you dont know how then i can help you with that. I hope this helps!
hey there,
here’s a example of how you would save a parts size, position, and other data / properties
you cant actually save the part instance its self you have to serialize the parts data and deserialize the data
you can also use methods like CFrame:ToWorldSpace() or CFrame:PointToWorldSpace()
if you wanted to save a parts data and wanted to load it on a players plot if you game has plots and you wanted to position the parts position relative to the plot or another part’s position
local DataStoreService = game:GetService("DataStoreService")
local datastore = DataStoreService:GetDataStore("datastore")
local block = workspace:WaitForChild("block")
function saveblockdata(block)
local blockdata = {}
blockdata["Size"] = block.Size -- Vector3
blockdata["Position"] = block.Position
blockdata["Color"] = block.Color
local success, errormessage = pcall(function()
datastore:SetAysnc("block", blockdata)
)
end
function loadblockdata()
local blockdata
local success, errormessage = pcall(function()
blockdata = datastore:GetAysnc("block")
end)
local block = Instance.new("Part")
block.Size = blockdata.Size
block.Position = blockdata.Position
block.Color = blockdata.Color
block.Parent = workspace
end
if you wanted to save a model then you just have to save its primary parts cframe
and when the player joins the game you can just clone the model from replicatedstorage
here’s another example
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
Players.PlayerAdded:Connect(function(player)
local savedmodel
local success, errormessage = pcall(function()
savedmodel = datastore:GetAysnc(player.UserId.."savedmodel")
end)
local newmodel = ReplicatedStorage[savedmodel.Name]:Clone()
newmodel:PivotTo(savedmodel.CFrame)
newmodel.Parent = workspace
end)
also you cant save instances you can only save numbers, tables, strings and bool values.
you can also learn about datastoreservice on the creatorhub
I’d put the model in the ServerStorage, and provide the model name in the DataStore.