How to save welds to datastore

Im making a building system,where player can build their vehicle but problem is that i dont know how to save welds to datastore, i can save parts,models but i have no idea how to save and then load weld

The old Weld or WeldConstraint?

In DataStore you can only save Number, String and Boolean values so saving welds isn’t possible. What you can do is save a table which contains info about part0 and part1 of the weld either by using a specific ID you give to the parts or using their name if it’s unique to each one.
Such table would look like this:

local Weld = {
[1] = --Part0 ID or Part0 Name,
[2] = --Part1 ID or Part1 Name
}
--Or 
local Weld = {
["Part0"] = --Part0 ID or Part0 Name,
["Part1"] = --Part1 ID or Part1 Name
}

It’s better to use the first one as it saves up more space
Hope this helped :grin:

1 Like

WeldConstraint

realrealrealreal

You need a way to save the Part0 and Part1 of the WeldConstraint. You cant save Instance values though, since those can’t be turned into a tring. Instead you need a unique way of identifying each possible part that could be a Part0/1. I have had to do something like this, and the way I did it was make sure each part has a unique name, then save a dotted string the same way you’d access an instance in the workspace:

Model
  Part1
    Part2
    Part4
  Part3
    Part2 --Allowed because the full path will still be unique

I believe this is the code I used. This will rename parts in a model so they all have a unique path. It will only rename Instances that already have a duplicate, so your scripts must already be accessing things in the model via unique paths.

--Make all workspace hierarchy paths unique so that parts can be located when saving and loaded!
local function CheckNamesUniqueRecursive(model : Instance)
	local renameNumber = 1
	local allNames = {}

	local function Rename(instance : Instance)
		instance.Name = "P" ..tostring(renameNumber)

		renameNumber += 1
	end
	
	for _, child in model:GetChildren() do
		if allNames[child.Name] ~= nil then
			--Name already taken, need to rename

			Rename(child)
		end

		allNames[child.Name] = child.Name

		--Recursive
		CheckNamesUniqueRecursive(child)
	end
end

These are for creating relative paths and evaluating them. It gives you a string like:
GetUniqueRelativePath(Part4, Model) → “Model.Part1.Part4”

To get the Instance Part4 back from this:
ResolveRelativePath(“Model.Part1.Part4”, Model) → Part4

function module.GetUniqueRelativePath(pathTo : Instance, from : Model) : string
	if not pathTo:IsDescendantOf(from) then
		error("Can't find relative path, pathTo Instance is not a descendant of 'from'")
	end

	local path = pathTo.Name
	local parent = pathTo.Parent

	while parent ~= from do
		path = parent.Name ..".".. path

		parent = parent.Parent
	end

	return path
end

function module.ResolveRelativePath(path : string, base : Instance) : Instance
	local names = string.split(path, ".")

	for _, pname in names do
		base = base[pname]
		
		if base == nil then
			error("Invalid relative path. Base instance: " ..tostring(base.Name).. " Path string: " ..tostring(path))
		end
	end

	return base
end

As for CFrames, there are ways to serialize them. You only need to record the position of one part of the weld relative to the other, not necessarily the CFrame of both parts. The way I do this is to give every model a PrimaryPart whose CFrame is the one used for all positioning, the other parts end up in the right place because the full Model is saved on the server and moved to this target CFrame. Heres how I did it: Base64 Encoding and Decoding in Lua - #2 by azqjanna