Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Saving and retrieving CFrame from a table?

Asked by 3 years ago

Essentially, I am trying to make it so mid-game, the player's character Rig Type can change. Despite successfully allowing the type of character to change, the Accessories position is not in place. To fix this, I am saving the CFrames in a table and then retrieving them to apply it to the new character. As far as I am concerned, CFrames contain commas, which would automatically separate indexes in the table rather than saving it as one total CFrame. Is there any way I can save it as one value so I can retrieve it later?

Here is the script that saves the values:

local charNames = {}
        local charCFrames = {}
        for a, b in pairs(plr.Character:GetChildren()) do
            if b:IsA("Accessory") then
                for c, d in pairs(b:GetChildren()) do
                    if d:IsA("Part") then
                        table.insert(charNames, #charNames, b.Name)
                        table.insert(charCFrames, #charCFrames, d.CFrame)
                    end
                end
            end
        end

Here is the script that retrieves the values:

local count = 0
for a, b in pairs(plr.Character:GetChildren()) do
    if b:IsA("Accessory") then
        for c, d in pairs(b:GetChildren()) do
            if d:IsA("Part") then
                for e, f in pairs(charNames) do
                    if d.Parent.Name == tostring(f) then
                        local newPos = charCFrames[count]
                        d.CFrame = newPos
                        print(newPos)
                    end
                    count = count +1
                end
            end
        end
    end
end
count = 0

Thank you in advance!

2 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

Serialize the CFrame in whichever way you prefer, tables or strings work. You do want to save it as one value so then a string is the best method here. My method is to serialize it into a string with spaces in between the numbers then when deserializing, those spaces serve to separate each coordinate number.

I prefer strings;

local function serializeCFrame(cf)
    local serializedString = ""
    local components = cf:GetComponents()
    for _, component in ipairs({components}) do
        serializedString = component.." "
    end
    return serializedString
end

And here is how you would deserialize it to get back the CFrame into a CFrame value:

local function deserializeCFrame(str)
    local t = table.create(12) -- a CFrame has 12 components
    for match in string.gmatch("%S+") do -- look for non-whitespace
        t[#t + 1] = match
    end
    return CFrame.new(unpack(t)) -- convert table to tuple
end
Ad
Log in to vote
0
Answered by
zadobyte 692 Moderation Voter
3 years ago

You can save each property seperately as so:

local saveCFrame = {}

local charCFrame = char.PrimaryPart.CFrame
saveCFrame["X"] = charCFrame.X
saveCFrame["Y"] = charCFrame.Y
saveCFrame["Z"] = charCFrame.Z

Answer this question