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:
01 | local 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:
01 | local count = 0 |
02 | for 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 |
17 | end |
18 | count = 0 |
Thank you in advance!
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;
1 | local 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 |
8 | end |
And here is how you would deserialize it to get back the CFrame into a CFrame value:
1 | local 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 |
7 | end |
You can save each property seperately as so:
1 | local saveCFrame = { } |
2 |
3 | local charCFrame = char.PrimaryPart.CFrame |
4 | saveCFrame [ "X" ] = charCFrame.X |
5 | saveCFrame [ "Y" ] = charCFrame.Y |
6 | saveCFrame [ "Z" ] = charCFrame.Z |