I’m trying to find the bottom of an object so that you can place it flat on a surface when you use a prompt.
I’ve been trying to halve the size of the object, and add it to the position of the surface. The problem is that I can not use a Size Vector3 value in conjunction with a Position Vector3. I should be able to use it if the Size Vector3 value were read as a pure number value and not a size value.
I’ve seen information about “Vector3Value” but it makes zero sense to me and it’s very odd to use.
Generally what I’ve been doing is something like taking the Position of the spot, and adding it to 1/2 the Size of the object, so it will be placed flat. I’m looking either for a way to do that, or for a separate way I can place an object flat on a surface.
This script when attached to an existing part, spawns a new part at it’s location (X,Z) but places the new part (Z) directly on top of the first part. This example should get you going in the right direction.
local part = script.Parent
print("Part is at: " .. tostring(part.Position))
print("Part Size is: " .. tostring(part.Size))
local midPointX = part.Size.X / 2
local midPointY = part.Size.Y / 2
local midPointZ = part.Size.Z / 2
print("Middle is at: X:" .. midPointX .. " Y:" .. midPointY .. " Z:" .. midPointZ)
local topEdgeY = part.Position.Y + midPointY
local bottomEdgeY = part.Position.Y - midPointY
print("Top: " .. topEdgeY)
print("Bottom: " .. bottomEdgeY)
local newPart = Instance.new("Part")
newPart.Parent = part
newPart.Anchored = true
newPart.Position = Vector3.new(part.Position.X, topEdgeY + newPart.Size.Y / 2, part.Position.Z)
Thank you, I’m not entirely sure what changed between these two, but I think the problem was that I was using a number in place of a Vector3.new? In any case, thank you for the answer, it got me there with some tinkering.
part.Position.X = someNumX --The X, Y, Z properties can't be modified directly
You can read/copy the individual values from the .Position,
local xValue = part.Position.X
but to change them you need to create a Vector3.new() with the new values and apply the whole vector to .Position. In this case part.Position is a Vector3 so it’s expecting a Vector3 as input.
--assign the Vector3 to the part directly
part.Position = Vector3.new(someNumX, someNumY, someNumZ)
You could also create the Vector3 elsewhere then reference it later if needed.
local newPosVector = Vector3.new(someNumX, someNumY, someNumZ)
--then assign later
part.Position = newPosVector
If a property is of a certain type like a Vector3 versus a basic/built-in type like a float, int, bool, string, etc… which you can generally edit directly, you have to create a .new of that type, with the new values you want and apply it to the object/part.