Help with CFrame:ToWorldSpace?

I am trying to create a part that spawns on the side of another part, someone has told me to use CFrame:ToWorldSpace. I used it, and it worked fine when only placing one part. then, I tried adding a second. when I did the first part was placed correctly, the second part was in the wrong spot. I do not know why, here is the code:

local Part = script.Parent

local Pos = Part.CFrame

local Size = Part.Size

local Arrow = Instance.new("Part")

Arrow.Parent = script.Parent

local SizeX = Size.X * 1/2

Arrow.Anchored = true

local FinalPos = SizeX + 1

Arrow.CFrame = script.Parent.CFrame:ToWorldSpace(CFrame.new(FinalPos, 0, 0))

Arrow.Size = Vector3.new(0.5, 0.5, 0.5)

local Part = script.Parent

local Pos = Part.CFrame

local Size = Part.Size

local Arrow = Instance.new("Part")

Arrow.Parent = script.Parent

local SizeX = Size.X * 1/2

Arrow.Anchored = true

local FinalPos = SizeX - 1

Arrow.CFrame = script.Parent.CFrame:ToWorldSpace(CFrame.new(FinalPos, 0, 0))

Arrow.Size = Vector3.new(0.5, 0.5, 0.5)

anyone know why this does not work? thanks in advance, me7474.

Did you mean to paste your code twice? Cause it seems to repeat.

Here’s a better formatted and organized version of your code, without the duplicate:

local part = script.Parent
local size = part.Size
local sizeX = size.X * 1 / 2
local finalPos = sizeX + 1

local arrow = Instance.new("Part")
arrow.Parent = part
arrow.Anchored = true
arrow.Size = Vector3.new(0.5, 0.5, 0.5)
arrow.CFrame = part.CFrame:ToWorldSpace(CFrame.new(finalPos, 0, 0))

Also removed the pos variable because you never use it.


Not sure what you mean. Which side do you want? And where on the side should it be? How should it be rotated?

If you just want to spawn a part on e.g. the right side of another part, you can do it like this:

local sideToPlaceOnId = Enum.NormalId.Right
local sideToPlaceOn = Vector3.FromNormalId(sideToPlaceOnId) --This just becomes (1, 0, 0)
local sizeInSideDir = (partToPlaceOn.Size * sideToPlaceOn).Magnitude --All axes except one become 0
local centerOfSideObjSpace = sideToPlaceOn * sizeInSideDir
local centerOfSideWorldSpace = partToPlaceOn.CFrame:PointToWorldSpace(centerOfSideObjSpace)
local sideToPlaceOnWorldSpace = partToPlaceOn.CFrame:VectorToWorldSpace(sideToPlaceOn)

local partToPlace = -- create your Part however you want
partToPlace.CFrame = CFrame.new(centerOfSideWorldSpace, centerOfSideWorldSpace + sideToPlaceOnWorldSpace)

It’s really long winded to really describe the logic of how it works. The stuff involving sideToPlaceOnWorldSpace is just to make the placed part also face outwards from the face it is placed on, although it doesn’t work completely.

Is this along the lines of what you’re looking for?

kind of, but I pasted the code twice because I was wanting to place an arrow on each side of the part, not just one, that way they could be used as a movement tool that players could drag around and move the part with. I already have everything else made, I just need practice on CFrames and need to brush up on my math. thank you though, that was helpful, as I can modify it to work on all sides.

1 Like