Issue when checking if the 2 attachments are on the same position

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

thanks in advance

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

1 Like

i actually let all results get printed, this came out

might become tricky to use return in a function that is supposed to carry out the entire loop on its own

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!

oh the range where i set the limit gets passed as parameter when calling the function

also thanks, didnt know something as “continue” would exist

Try printing the range, there is likely a problem there as the distance is calculated correctly.

Continue is very useful, took me a while to realize it existed, but it does help a lot :slight_smile:

might be usefull to also say that the parameter “range” is 0.1, forgot to say it before

or what do you mean by range

Oh. All of these:

are a lot larger than range.

This means that they should skip with all current values

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.

(pink block)

1 Like

How many are you expecting to be created?

i expect 7 to be created.

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!

Side-note: never realised that was called a guard clause!

Thanks for the info!

2 Likes

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.

1 Like

I’ve found the error, it was an issue with the debounce table.

@ifkpop thank you very much for your patience and help

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.