Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function deserializeEnum(str)
- local stack = getfenv()
- for part in str:gmatch("[^.]+") do
- stack = stack[part]
- end
- return stack
- end
- function serializeKFS(kfs)
- local http = game:GetService("HttpService")
- local data = {}
- data.Name = kfs.Name
- data.Loop = kfs.Loop
- data.Priority = tostring(kfs.Priority)
- data.Keyframes = {}
- local function registerPose(pose,parent)
- local sPose = {}
- sPose.Name = pose.Name
- sPose.CFrame = {pose.CFrame:components()}
- sPose.Weight = pose.Weight
- sPose.MaskWeight = pose.MaskWeight
- sPose.SubPoses = {}
- for _,subPose in pairs(pose:GetChildren()) do
- registerPose(subPose,sPose.SubPoses)
- end
- table.insert(parent,sPose)
- end
- local keyframes = kfs:GetChildren()
- table.sort(keyframes,function (a,b)
- return a.Time < b.Time
- end)
- for index,keyframe in pairs(keyframes) do
- local sKeyframe = {}
- sKeyframe.Name = keyframe.Name
- sKeyframe.Time = keyframe.Time
- sKeyframe.Poses = {}
- for _,pose in pairs(keyframe:GetChildren()) do
- registerPose(pose,sKeyframe.Poses)
- end
- data.Keyframes[index] = sKeyframe
- end
- return http:JSONEncode(data)
- end
- function deserializeKFS(dataStr)
- local http = game:GetService("HttpService")
- local data = http:JSONDecode(dataStr)
- local kfs = Instance.new("KeyframeSequence")
- kfs.Name = data.Name
- kfs.Loop = data.Loop
- kfs.Priority = deserializeEnum(data.Priority)
- local function registerPose(sPose,parent)
- local pose = Instance.new("Pose")
- pose.Name = sPose.Name
- pose.CFrame = CFrame.new(unpack(sPose.CFrame))
- pose.Weight = sPose.Weight
- pose.MaskWeight = sPose.MaskWeight
- for _,sSubPose in pairs(sPose.SubPoses) do
- registerPose(sSubPose,pose)
- end
- pose.Parent = parent
- end
- for _,sKeyframe in pairs(data.Keyframes) do
- local keyframe = Instance.new("Keyframe")
- keyframe.Name = sKeyframe.Name
- keyframe.Time = sKeyframe.Time
- for _,sPose in pairs(sKeyframe.Poses) do
- registerPose(sPose,keyframe)
- end
- keyframe.Parent = kfs
- end
- return kfs
- end
- local serialized = serializeKFS(workspace.Test)
- print(serialized)
- local deserialized = deserializeKFS(serialized)
- deserialized.Parent = workspace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement