What is tobjectspace

HI DEVS,

what is ToObjectSpace i know what ToWorldSpace is and i have searched for answers but didnt understand any

1 Like

He said he knew what toworldspace is, he doesn’t know what toobjectspace is.

1 Like

It converts a world space coordinate to an object space coordinate relative to whatever CFrame you use for conversion.

Let’s take this for example:

local Object = CFrame.new(0, 5, 5)
local Point = CFrame.new(0, 6, 6)

local Offset = Object:ToObjectSpace(Point)
print(Offset.Position) -- > 0, 1, 1

The ‘Point’ is at position [0, 6, 6] relative to the world origin, but is at [0, 1, 1] relative to the ‘Object’ (it’s one stud higher on the Y axis and one stud further on the Z axis, relative to 'Object’s position.

This applies to rotation as well, however, I only used position for simplicity of demonstration.

Let me know if this makes sense to you.

1 Like
local point1 = vector3.new(0,0,2)
local point2 = vector3.new(0,0,1)

print(point1:ToObjectSpace(Point2))

will the output be 1?

It will print other stuff as well but yea the offset on the Z axis of the new point will be -1.

so can you give me an example on how i would use this

I made a video on this topic linked here

2 Likes

i watched your video before i didn’t learn the ToObjectSpace part but it is where i learned ToWorldSpace

I feel like I use ToWorldSpace() a LOT more than ToObjectSpace(), but in summary toobject space converts the global world coordinates into something relative to the object.

For example, if you want to save someone’s plot in a game, but you don’t know where the plot will be when they rejoin, you can save the object space in order to load it later:

local coords = {}
for _, part in ipairs(plot:GetChildren() do
  local pos = pos:GetPrimaryPartCFrame():ToObjectSpace(part.CFrame)
  coords[part.Name] = pos
end

And then when you load it, you can do:

for name, pos in pairs(coords) do
  local part = --create new part here
  part.Name = name
  part.CFrame = newPlot:ToWorldSpace(pos)
  part.Parent = workspace
end

This example is incomplete but you get the idea. Hope this helps!

1 Like

i wanna get things strait so to object space returns a vector and it just subtracts the vectors?

Yes, but not Vectors, CFrames. use PointToObjectSpace for vectors

1 Like

i still dont get on how i would use this

Don’t worry! You will never find a use if you try to explicity use it. Just keep this information in the back of your mind, and next time you are stumped on a CFrame problem, just try using it. That’s how I learned, I knew the api, but whenever I got stuck, I just slotted a ton of functions in until one got the desired effect. Eventually, you will get to know the right tool for the job.

1 Like