How can I json encode CFRame?

Hello, I’m Eli. I’m working on a cutscene plugin, and ran into a problem. Whenever I use httpservice to jsonencode CFrame, it always returns “null”. Is there a way to fix this?

function createplayeradded()
	local clone = script.Parent:WaitForChild('Scripts'):WaitForChild("PlayerAdded"):Clone()
	clone.Parent = workspace
	clone.Disabled = false
	clone.Cutscene:WaitForChild('CutsceneKeyframesJSON').Value = game:GetService('HttpService'):JSONEncode(keyframes)
	clone.Cutscene:WaitForChild('CutsceneTimesJSON').Value = game:GetService('HttpService'):JSONEncode(keyframetimes)
	closesaveoptions()
	selected:Set(clone)
	wait()
	for i = 1,#keyframes do
		table.remove(keyframes,i)
		table.remove(keyframetimes,i)
		
	end
	wait()
	keyframesvalue = #keyframes
end

That’s the code I’m trying to fix, and I have no clue how to get started. Any help would be appreciated. Thanks!

1 Like

You could make a function to encode cframes, it would end up looking something like this.

function EncodeCFrame(cfr)
	return {cfr:components()}
end

After that you should be able to use that function to successfully json encode the CFrame.

local cfr = EncodeCFrame(CFrame.new(1, 1, 1))
local encoded = game:GetService("HttpService"):JSONEncode(cfr)

print(encoded)
8 Likes

and to decode…

function DecodeCFrame(tbl)
    return CFrame.new(unpack(tbl))
end

local decoded = game:GetService("HttpService"):JSONDecode(str)
local cfr = DecodeCFrame(decoded)

print(cfr)
7 Likes

How do I mark two as a solution?! :disappointed_relieved:

1 Like

You want to mark @Jhxan as solution, as theirs was the answer to the question you asked. Mine was just companion code :+1:

1 Like

I have another question now, how would I replace all the stuff in the table with it encoded? It would just make it more convienient

You would just use @Jhxan’s code on any CFrame values. There’s no CFrame values in the sample you provided, but if there are children of keyframes and keyframetimes that have CFrame values you’d like to encode, you would need to iterate through the children and store their values in a separate table, using :GetChildren().

That’s what I thought, I just though there might be a more convienient way.