Adding a border around parts via script

Hello! Hope everyone is doing well.

I have been trying to make a script where it adds a border around a part with a script with the width set to a variable . However, I have been debugging for 1.5 hours so far and I just couldn’t get it to work. It got so bad, where I had to ask ChatGPT and not even it could fix it.

If anyone needs the size of the part I want the border set to, it is: 7, 1, 14

local mainPart = game.Workspace.StuddedPart

local studWidth = 1 -- width of the border

local function addBorder(x : BoolValue, z : BoolValue)
	if x then
		-- fill out later
	end
	
	if z then
		local offset = -2.5 + (1.5 * (studWidth - 1)) -- formula (not working)
		
		local borderPos = mainPart.CFrame + Vector3.new(0, 0, offset) -- gets the position of the border
		
		local p = Instance.new('Part')
		p.Parent = game.Workspace
		p.Size = Vector3.new(mainPart.Size.X, mainPart.Size.Y, studWidth) -- Makes the width of the border be studLength
		p.CFrame = borderPos -- sets the border part to the border position
	end
end

addBorder(false, true) -- For now, only sets the border on the Z axis

On the image above, you can see that the part is 5 studs away from where it is supposed to be. I don’t know if it is the formula or it is just roblox at this point.

I would mess with the formula offset , since it is completely broken.

This entire script is supposed to be redone into a plugin so it could make my life easier for an obby I am making, without having to manually change the size every time.

If anyone has any solutions or recommendations, I would love to hear you out!

I would just calculate offset based off of the part, to make your function more widely applicable.

local function addBorder(part:Part,x:boolean,y:boolean,borderWidth:number?): nil --assumes part is not rotated at all
	local borderWidth:number = borderWidth or 1
	
	if x then
		local offset:number = (part.Size.Z/2)+(borderWidth/2)
		
		for i=1,2 do --make both parts
			local borderPart:Part = Instance.new("Part")
			borderPart.Anchored = true
			borderPart.Size = Vector3.new(part.Size.X+(y and borderWidth*2 or 0),part.Size.Y,borderWidth)
			borderPart.CFrame = part.CFrame+Vector3.new(0,0,offset*(-1)^i)
			
			borderPart.Parent = workspace
		end
	end
	
	if y then
		local offset:number = (part.Size.X/2)+(borderWidth/2)

		for i=1,2 do --make both parts
			local borderPart:Part = Instance.new("Part")
			borderPart.Anchored = true
			borderPart.Size = Vector3.new(borderWidth,part.Size.Y,part.Size.Z)
			borderPart.CFrame = part.CFrame+Vector3.new(offset*(-1)^i,0,0)

			borderPart.Parent = workspace
		end
	end
	
	return nil
end

This will only work if ur part has no rotation. If u wanted to account for rotation, you could do so with some basic trig.

Wow, thank you so much! You are a life saver. This has been a problem of mine for 2 days right now and I couldn’t solve it.

Have an amazing rest of your day!

1 Like