Wall is scaled strangely

This code makes a wall.
It uses magnitude to create the size (so it stretches from point 1 to point 2)
except since roblox is weird, it scales magnitude in both directions
for instance if the part was 5 studs away
the wall scales in both directions of Z. Hope that makes sense


local m = game.Players.LocalPlayer:GetMouse()
local debounce = false
local Position = nil
m.Button1Down:connect(function(k)
	Position = m.Hit.Position
	wait(1)
end)
m.Button1Up:connect(function(k)
	local Position2 = m.Hit.Position
	local wall = Instance.new("Part")
	wall.Position = Position
	wall.Parent = workspace
	wall.Anchored=true
	local magnitude = (wall.Position-Position2).Magnitude
	wall.Size = Vector3.new(0.5,1,magnitude)
	while true do
		wall.CFrame = CFrame.new(wall.Position, Position2)
		wait()
	end
	
end)
1 Like

Make it so it moves it in the direction you want it by the size of the part divided by 2

I’ll try this. Give me a moment

How would I divide the size of the part? Do I just do wall.Size/2?

This doesn’t seem to work.
I’m probably doing something wrong

wall.Position = Position*wall.Size/2

dont multiply it i mean add it to a specific vector3 axis like wall.Position.X + wall.Size.X/2
or whatever axis you need it to be
oh yeah like @TheSenorDuck said i made a mistake, get a particular axis for the size of the wall to be added to a specific axis on the position vector of the wall.

You have to use wall.Size.X or wall.Size.Y or wall.Size.Z instead of using wall.Size

1 Like

Still seems to have the same issue.

wall.Position = Position*wall.Size.X/2

i just applied this to the script
theres no change

wall.Position = Vector3.new(Position.X + wall.Size.X/2)

try wall.Position = Vector3.new(Position.X + wall.Size.X/2, position.Y, position.Z)

From what I understand you want to make a wall from the position where you pressed down your mouse to the position where you let go of your mouse

To make the wall face from p1 to p2 you just do wall.CFrame = CFrame.lookAt(p1, p2) and then you multiply that cframe by CFrame.new(0, 0, -wall.Size.Z / 2) to move the wall forward by half of the wall’s length so it’s between p1 and p2. For the size it’s just the magnitude between p1 and p2 (you have that correct for the most part)

Also I’m not sure why you have a while loop in there

You need to use + instead of * I think

Testing this right now.
Wait a moment

Same result.
Is my code wrong or something?


local m = game.Players.LocalPlayer:GetMouse()
local debounce = false
local Position = nil
m.Button1Down:connect(function(k)
	Position = m.Hit.Position
	wait(1)
end)
m.Button1Up:connect(function(k)
	local Position2 = m.Hit.Position
	local wall = Instance.new("Part")
	wall.Parent = workspace
	wall.Anchored=true
	local magnitude = (wall.Position-Position2).Magnitude
	wall.Size = Vector3.new(0.5,1,magnitude)
	wall.Position = Position
	CFrame.new(0, 0, -wall.Size.Z / 2)
	wall.CFrame = CFrame.lookAt(Position,Position2)
	
end)
--|-|_|_

Yes

local magnitude = (Position-Position2).Magnitude
wall.CFrame = CFrame.lookAt(Position,Position2) * CFrame.new(0, 0, -wall.Size.Z / 2)

You also don’t need to set the wall’s position because you set the position and rotation with cframe on the next line

One last question
How do I make it not face into the floor?
Since cframe:Lookat() uses 3D (XYZ when I want it to only be XZ) it can also face downwards towards the point.

You could raycast down from the midpoint of “Position” and “Position2” and set their Y coordinate to the raycast’s Y coordinate. Or just use the lowest Y coordinate out of the two positions if you’re building on flat terrain

I don’t know how you want your wall placement to behave so I don’t know the best way to help

Yes, but how do I move only one axis?
When I try doing it it says “Y cannot be changed” or something like that

You need to make a new vector3 with the old vector’s X and Z and your new Y, e.g. p = Vector3.new(p.X, 10, p.Z)

You can’t directly set the vector’s coordinates like Position.Y = 10

This works, thanks alot!
Thanks to all of you who replied too, sorry I can only give one solution