Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do a save a model in a data store?

Hey all. I'm making a building based game and need some help with saving the players creations to get loaded in when they join the game. Any help will be greatly appreciated. I've looked online and can't find anything that suits my needs. Thanks in advance, Tim.

2 comments

0

If that doesn't work, here. https://roblox.fandom.com/wiki/Data_type If you need any more help, I'll be happy to find more links. :)

1 answer

✓ Accepted answer

you can't save models inside datastores

you can, however, save the important data about those models in a table

that includes things like position, size, color, material, etc, which can all easily be stored (with a little bit of work, since only primitive types, aka booleans, strings, tables, numbers, and nil, can be stored)

you could do that with something like this, but if you aren't familiar with tables, you should get to know them first — not just because they're used here, they're really really useful in general

-- this script assumes that the player is only able to place parts btw, if you want to add other classes you'll have to do that yourself
local modelDescription = {} -- make a table to describe the model's children

local function getChildDescription(child)
	
	local childDescription = {} 
	childDescription.CFrame = {child.CFrame:GetComponents()} -- we do this since a CFrame can't directly be stored in a DataStore, but an array of numbers can
	childDescription.Size = {child.Size.X, child.Size.Y, child.Size.Z} -- Vector3s can't be stored either
	childDescription.Color = {child.Color.r, Child.Color.g, child.Color.b} -- or Color3s...
	childDescription.Material = child.Material.Name -- the Material property is an enum, not a string, so we have to do this
	childDescription.Reflectance = child.Reflectance
	childDescription.Transparency = child.Transparency
	childDescription.Anchored = child.Anchored
	childDescription.CanCollide = child.CanCollide
	childDescription.Children = {} 
	for _, child2 in pairs(child:GetChildren()) do
		table.insert(childDescription.Children, getChildDescription(child2))
	end

end

for _, child in pairs(model:GetChildren()) do
	table.insert(modelDescription, getChildDescription(child))
end

-- the loading part

local modelDescription = datastore get blah blah blah

local function loadChild(parent, description)
	local newPart = Instance.new("BasePart")
	newPart.CFrame = CFrame.new(unpack(description.CFrame)) -- unpack, well, unpacks a table into just the values
	newPart.Size = Vector3.new(unpack(description.Size))
	newPart.Color = Color3.new(unpack(description.Color))
	newPart.Material = description.Material
	newPart.Reflectance = description.Reflectance
	newPart.Transparency = description.Transparency
	newPart.Anchored = description.Anchored
	newPart.CanCollide = description.CanCollide
	for _, childDescription in pairs(description.Children) do
		loadChild(newPart, childDescription)
	end
	newPart.Parent = parent
end

local model = Instance.new("Model")
for _, childDescription in pairs(modelDescription) do
	loadChild(model, childDescription)
end
model.Parent = workspace

1 comment

0

Sorry for a 2 month late response, but

This discussion is preserved as it was. New answers, comments, and votes are closed.