I’ve been trying to make a script that automatically creates constraints by searching for attachments with a specific name and then comparing their position. However for some reason it doesn’t seem to properly understand if they have a certain required maximal distance from eachother, and then skips them falsely.
for _, subattachment in ipairs(attachments) do
if attachment ~= subattachment and attachment.Name == subattachment.Name then
warn((attachment.WorldCFrame.Position - subattachment.WorldCFrame.Position).Magnitude)
if (attachment.WorldCFrame.Position - subattachment.WorldCFrame.Position).Magnitude <= range then
if #debounce <= 0 then
table.insert(debounce, functions.createAttachment(attachment.Name, attachment, subattachment))
else
local alreadyexists = false
for _, alr_constraint in ipairs(debounce) do
if not (alr_constraint.Attachment0 == attachment and alr_constraint.Attachment1 == subattachment) and not (alr_constraint.Attachment0 == subattachment and alr_constraint.Attachment1 == attachment) then
alreadyexists = true
end
end
if not alreadyexists then
table.insert(debounce, functions.createAttachment(attachment.Name, attachment, subattachment))
end
end
end
end
end
I’ve already narrowed down the problem, its this line:
if (attachment.WorldCFrame.Position - subattachment.WorldCFrame.Position).Magnitude <= range then
Math looks correct, have you tried printing the range & the calculated distance?
Also, consider implementing guard clauses instead, it makes code a lot more readable!
Example:
for _, subattachment in ipairs(attachments) do
-- De-nest the code (guard clauses, negation of their original logic)
if attachment == subattachment or attachment.Name ~= subattachment.Name then continue end
if (attachment.WorldCFrame.Position - subattachment.WorldCFrame.Position).Magnitude > range then continue end
warn((attachment.WorldCFrame.Position - subattachment.WorldCFrame.Position).Magnitude)
if #debounce <= 0 then
table.insert(debounce, functions.createAttachment(attachment.Name, attachment, subattachment))
else
local alreadyexists = false
for _, alr_constraint in ipairs(debounce) do
if not (alr_constraint.Attachment0 == attachment and alr_constraint.Attachment1 == subattachment) and not (alr_constraint.Attachment0 == subattachment and alr_constraint.Attachment1 == attachment) then
alreadyexists = true
break -- No need to continue looping after it has been determined to exist
end
end
if not alreadyexists then
table.insert(debounce, functions.createAttachment(attachment.Name, attachment, subattachment))
end
end
end
Edit: accidentally wrote return instead of continue
I’m guessing that is the distance between all attachments? Do you at any point print what the range is? I don’t see anything wrong with how the distance is calculated.
My bad with this, forgot it was in a loop, but the principle still applies. Replace return with continue and it should work!
That is indeed right. That is why only 2 constraints (out of 7) get created. The length, in which this constraints are positioned is arround 400 studs. I was originally planning on merging a train using this script.
I’m sorry but i will need to leave in 5 minutes thats why i wont be able to answer in the next 2 hours, however thanks for the help you provided until now!
I can’t really find any issues in the code you’ve provided, are you certain that the tables debounce and attachments are correct? They may be sources of error.
i’m getting the attachment array using this function
function functions.getAttachments(model, ValidNames)
local Attachments = {}
for _, instance in ipairs(model:GetDescendants()) do
if instance:IsA("Attachment") then
if table.find(ValidNames, instance.Name) then
table.insert(Attachments, instance)
end
end
end
return Attachments
end
and the “debounce” array is an array that gets filled in the function i sent in the original post, it looks that no constraint gets created twice
Hm, looks like the attachments would be correct. I think the original code you sent would be correct, but you should verify that it contains all attachments you want to check with, and that all attachments you want checked with each other are checked.
I sadly don’t have a better tip than this; if you require any further help, just ping me and I’ll try to respond whenever I got the time to do so.