Struggling with CFrame?

Hello I am making a level editor for my game CaseOh’s basics.
Parts work good, but not models.

This is how I get the “CFrame” of a model (no I can’t use primaryPart.CFrame for some reason)

object.CF_={v.PrimaryPart.CFrame.Position.X..","..v.PrimaryPart.CFrame.Position.Y..","..v.PrimaryPart.CFrame.Position.Z,v.PrimaryPart.Orientation.X..","..v.PrimaryPart.Orientation.Y..","..v.PrimaryPart.Orientation.Z}

And when I go and apply this to the model it is slightly off.

local rotate=CFrame.Angles(table.unpack(string.split(object.CF_[2],",")))
				local targetCFrame=CFrame.new(table.unpack(string.split(object.CF_[1],","))) * rotate

I am probably making a dumb mistake but please someone tell me!! CF_[1] is position and 2 is orientation just like I showed previously.
Thanks for reading and I hope yall can help.

Have you tried model:GetPivot() ?

Storing the CFrame

First, let’s store the CFrame components more clearly and correctly:

object.CF_ = {
    position = v.PrimaryPart.Position,
    orientation = v.PrimaryPart.Orientation
}

Applying the CFrame

When you want to apply the CFrame back to the model, you should construct the CFrame and set it properly for all parts:

-- Parse the stored position and orientation
local position = Vector3.new(table.unpack(string.split(object.CF_.position, ",")))
local orientation = Vector3.new(table.unpack(string.split(object.CF_.orientation, ",")))

-- Create the target CFrame
local targetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(orientation.X), math.rad(orientation.Y), math.rad(orientation.Z))

-- Move the model to the target CFrame
local primaryPart = object.PrimaryPart

-- Calculate the offset for each part relative to the primary part
for _, part in ipairs(object:GetDescendants()) do
    if part:IsA("BasePart") then
        local offset = primaryPart.CFrame:toObjectSpace(part.CFrame)
        part.CFrame = targetCFrame:toWorldSpace(offset)
    end
end

I tried this but I get this error:

--invalid argument #1 to 'split' (string expected, got nil)

on this line:

local position = Vector3.new(table.unpack(string.split(object.CF_.position, ",")))

Sorry the string store them first

object.CF_ = {
    position = v.PrimaryPart.Position,
    orientation = v.PrimaryPart.Orientation
}

Applying

-- Directly use the stored position and orientation
local position = object.CF_.position
local orientation = object.CF_.orientation

-- Create the target CFrame
local targetCFrame = CFrame.new(position) * CFrame.Angles(math.rad(orientation.X), math.rad(orientation.Y), math.rad(orientation.Z))

-- Move the model to the target CFrame
local primaryPart = object.PrimaryPart

-- Calculate the offset for each part relative to the primary part
for _, part in ipairs(object:GetDescendants()) do
    if part:IsA("BasePart") then
        local offset = primaryPart.CFrame:ToObjectSpace(part.CFrame)
        part.CFrame = targetCFrame:ToWorldSpace(offset)
    end
end

I also tried printing CF_ but it returned as a blank table? This is the same thing that happened when I just straight up was setting the CF_ to .CFrame. I think the reason is that we’re setting the CF_ to a Vector3 value and not a string like I did here with a BasePart:

object.CF_={v.CFrame.Position.X..","..v.CFrame.Position.Y..","..v.CFrame.Position.Z,v.Orientation.X..","..v.Orientation.Y..","..v.Orientation.Z}

Maybe something like a table

local cf = v.CFrame
local newCF = {
    ["X"] = cf.Position.X,
    ["Y"] = cf.Position.Y,
    ["Z"] = cf.Position.Z,
    ["Orientation.X"] = cf.Orientation.X,
    ["Orientation.Y"] = cf.Orientation.Y,
    ["Orientation.Z"] = cf.Orientation.Z,
}

object.CF_ = newCF
1 Like

I just get this now:

--attempt to index nil with 'Position'

This is the current code:

	local cf = v.CFrame
				local newCF = {
					["X"] = cf.Position.X,
					["Y"] = cf.Position.Y,
					["Z"] = cf.Position.Z,
					["Orientation.X"] = cf.Orientation.X,
					["Orientation.Y"] = cf.Orientation.Y,
					["Orientation.Z"] = cf.Orientation.Z,
				}

				object.CF_ = newCF
				
				local targetCframe=CFrame.new(object.CF_.X, object.CF_.Y, object.CF_.Z) * CFrame.Angles(object.CF_["Orientation.X"], object.CF_["Orientation.Y"], object.CF_["Orientation.Z"])
				clone:PivotTo(targetCframe)

Edit:
This works with position:

clone:SetPrimaryPartCFrame(CFrame.new(table.unpack(string.split(object.CF_[1],","))))

But I just need to make it rotate too! I don’t know how. :sob:

I tested this:

	clone:SetPrimaryPartCFrame(CFrame.new(table.unpack(string.split(object.CF_[1],",")))*CFrame.Angles(table.unpack(string.split(object.CF_[2],","))))

it kind of works except the models are rotated a bit off, which is the same issue we started with.

local targetCframe = CFrame.new(object.CF_.X, object.CF_.Y, object.CF_.Z) * CFrame.Angles(object.CF_["Orientation.X"], object.CF_["Orientation.Y"], object.CF_["Orientation.Z"])
clone:PivotTo(targetCframe)

The solution was this:

	if object.CF_[2]~="0,0,0" then
					-- it can only be 0,90,0 or 0,-90,0 or 0,180,0 or 0,-180,0, lets have preset rotations for those!
					if object.CF_[2]=="0,90,0" then
						print("90")
						clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(90),0))
						
					elseif object.CF_[2]=="0,-90,0" then
						print("-90")
						-- lets rotate the model
						clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(-90),0))

					elseif object.CF_[2]=="0,180,0" then
						print("180")
						clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(180),0))
						
					elseif object.CF_[2]=="0,-180,0" then
						print("-180")
						clone:SetPrimaryPartCFrame(clone:GetPrimaryPartCFrame() * CFrame.Angles(0,math.rad(-180),0))
					else
						print(object.CF_[2])
					end
					
				end

Kinda bad solution but atleast it works lol Thanks for helping me you’re a W :sunglasses:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.