#ball attachent script
1 messages · Page 1 of 1 (latest)
local parts = {}
for i,v in pairs(script.Parent:GetDescendants()) do
if v:IsA("Part") then
table.insert(parts, v)
end
end
local function ball(part1,part2)
local attch1 = Instance.new("Attachment")
local attch2 = Instance.new("Attachment")
local ball = Instance.new("BallSocketConstraint")
local pos = part2.Position-part1.Position
ball.MaxFrictionTorque = 10000
attch1.CFrame = CFrame.new(pos)
attch2.CFrame = CFrame.new(pos)
ball.Attachment0 = attch2
ball.Attachment1 = attch1
ball.Parent = part1
attch2.Parent = part1
attch1.Parent = part2
end
for i,part1 in pairs(parts) do
ball(part1,script.Parent.MainPart)
end
car ends up like this
for _, v in pairs(script.Parent:GetDescendants()) do
if v:IsA("BasePart") and v ~= script.Parent.MainPart then
table.insert(parts, v)
end
end
local function weldParts(partA, partB)
-- Create attachments in each part
local attA = Instance.new("Attachment")
local attB = Instance.new("Attachment")
attA.Name = "BallAttachA"
attB.Name = "BallAttachB"
attA.Parent = partA
attB.Parent = partB
-- Align attachments to their respective part's center
attA.Position = Vector3.new(0, 0, 0)
attB.Position = partA.CFrame:PointToObjectSpace(partB.Position)
-- Create the BallSocketConstraint
local ball = Instance.new("BallSocketConstraint")
ball.Name = "BallConstraint_" .. partA.Name
ball.Attachment0 = attA
ball.Attachment1 = attB
ball.MaxFrictionTorque = 10000
ball.Parent = partA
end
-- Connect all parts to MainPart
for _, part in ipairs(parts) do
weldParts(part, script.Parent.MainPart)
end```