I’m making a hitbox system which requires attachments to be placed throughout a part. My friend helped me throw together some barebones code, but it’s pretty messy, and I feel could use some rewriting. I’m not too good with vectors though, so I’d like some reviewing.
What does your code do?
- Create attachments going from the origin point of the part, going up and down the X axis, Z axis, and Y axis.
Code
local function makeAttachment(Parent, Position)
local Attachment = Instance.new("Attachment")
Attachment.Parent = Parent
Attachment.Position = Position
return Attachment
end
local Quantity = 4
local Core = makeAttachment(Part, Vector3.new(0, 0, 0))
Core.Name = "0"
-- x axis
for i = 1, 4 do
if i > 1 then
local prevAttach = Part:FindFirstChild("X Attachment "..i - 1)
local Position = Vector3.new((Part.Size.X / 2 / Quantity + prevAttach.Position.X), 0, 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "X Attachment "..i
print(Attachment.Position)
else
local Position = Vector3.new((Part.Size.X / 2 / Quantity), 0, 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "X Attachment "..i
print(Attachment.Position)
end
end
-- minus x axis
for i = 1, 4 do
if i > 1 then
local prevAttach = Part:FindFirstChild("-X Attachment "..i - 1)
local Position = Vector3.new((-Part.Size.X / 2 / Quantity - -prevAttach.Position.X), 0, 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "-X Attachment "..i
print(Attachment.Position)
else
local Position = Vector3.new((-Part.Size.X / 2 / Quantity), 0, 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "-X Attachment "..i
print(Attachment.Position)
end
end
-- y axis
for i = 1, 4 do
if i > 1 then
local prevAttach = Part:FindFirstChild("Y Attachment "..i - 1)
local Position = Vector3.new(0, (Part.Size.Y / 2 / Quantity + prevAttach.Position.Y), 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "Y Attachment "..i
print(Attachment.Position)
else
local Position = Vector3.new(0, (Part.Size.Y / 2 / Quantity), 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "Y Attachment "..i
print(Attachment.Position)
end
end
-- minus y axis
for i = 1, 4 do
if i > 1 then
local prevAttach = Part:FindFirstChild("-Y Attachment "..i - 1)
local Position = Vector3.new(0, (-Part.Size.Y / 2 / Quantity - -prevAttach.Position.Y), 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "-Y Attachment "..i
print(Attachment.Position)
else
local Position = Vector3.new(0, (-Part.Size.Y / 2 / Quantity), 0)
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "-Y Attachment "..i
print(Attachment.Position)
end
end
-- z axis
for i = 1, 4 do
if i > 1 then
local prevAttach = Part:FindFirstChild("Z Attachment "..i - 1)
local Position = Vector3.new(0, 0, (Part.Size.Z / 2 / Quantity + prevAttach.Position.Z))
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "Z Attachment "..i
print(Attachment.Position)
else
local Position = Vector3.new(0, 0, (Part.Size.Z / 2 / Quantity))
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "Z Attachment "..i
print(Attachment.Position)
end
end
-- minus z attachment
for i = 1, 4 do
if i > 1 then
local prevAttach = Part:FindFirstChild("-Z Attachment "..i - 1)
local Position = Vector3.new(0, 0, (-Part.Size.Z / 2 / Quantity - -prevAttach.Position.Z))
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "-Z Attachment "..i
print(Attachment.Position)
else
local Position = Vector3.new(0, 0, (-Part.Size.Z / 2 / Quantity))
local Attachment = makeAttachment(Part, Position)
Attachment.Name = "-Z Attachment "..i
print(Attachment.Position)
end
end
Place File
attachment test.rbxl (22.8 KB)