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 4 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:

01local charNames = {}
02        local charCFrames = {}
03        for a, b in pairs(plr.Character:GetChildren()) do
04            if b:IsA("Accessory") then
05                for c, d in pairs(b:GetChildren()) do
06                    if d:IsA("Part") then
07                        table.insert(charNames, #charNames, b.Name)
08                        table.insert(charCFrames, #charCFrames, d.CFrame)
09                    end
10                end
11            end
12        end

Here is the script that retrieves the values:

01local count = 0
02for a, b in pairs(plr.Character:GetChildren()) do
03    if b:IsA("Accessory") then
04        for c, d in pairs(b:GetChildren()) do
05            if d:IsA("Part") then
06                for e, f in pairs(charNames) do
07                    if d.Parent.Name == tostring(f) then
08                        local newPos = charCFrames[count]
09                        d.CFrame = newPos
10                        print(newPos)
11                    end
12                    count = count +1
13                end
14            end
15        end
16    end
17end
18count = 0

Thank you in advance!

2 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 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;

1local function serializeCFrame(cf)
2    local serializedString = ""
3    local components = cf:GetComponents()
4    for _, component in ipairs({components}) do
5        serializedString = component.." "
6    end
7    return serializedString
8end

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

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

You can save each property seperately as so:

1local saveCFrame = {}
2 
3local charCFrame = char.PrimaryPart.CFrame
4saveCFrame["X"] = charCFrame.X
5saveCFrame["Y"] = charCFrame.Y
6saveCFrame["Z"] = charCFrame.Z

Answer this question