How to use :ToObjectSpace()

Hi! I am new to CFrame functions and I am curious why my script doesn’t work as expected. My goal is to instance parts, which will respect my first’s part orientation.
Here is a video of my problem: robloxapp-20200425-1050487.wmv (1.8 MB)

--------- CREATING A PART -----------------------------
local firstPart = Instance.new("Part", game.Workspace.squere)
firstPart.Name = "First"
firstPart.Anchored = true
firstPart.Position = Vector3.new(10, 12, 30)
local Size = 64
firstPart.Size = Vector3.new(math.sqrt(Size), 1, math.sqrt(Size)) 
wait(15)
---------- CLONING PARTS ACCORDING TO FIRST PART -------------
for i = 1, math.sqrt(Size) - 1, 1 do
	local clone = game.Workspace.squere.First:Clone()
	clone.Name = "firstClone"
	clone.Parent = game.Workspace.squere
	clone.Color = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))
	clone.CFrame = clone.CFrame:ToObjectSpace(game.Workspace.squere.First.CFrame) * CFrame.new(math.sqrt(Size) * i, 0, 0)  -- THIS LINE 
end 

Thanks for help!

1 Like

:ToObjectSpace() converts a CFrame relative to the world relative to a specific part.
For example let’s say object A is at 0, 10, 0 and object B is at 0, 20, 0. If we ran: print(objectA.CFrame:ToObjectSpace(objectB.CFrame) it would print 0,10,0 as object A is 10 studs above object B. It’s treating object B’s CFrame as the center of the world.

Credits to @AxoLib for the explanation, it’s from another post.

2 Likes