Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I find all of the parts in a model with the same Y value?

I want to union all of the parts with the same Y axis and have a certain value (Weight) in a model, but i'm not sure how to do it

local Print = workspace.GameValues.MagicDebug

local roundDecimals = function(num, places) 
--This Part Works (You don't need to see it and it's just a visual thing)
end

if Print then print(script.Parent.Name.."'s Union Function Begin") end
for i, part in pairs(script.Parent:GetChildren()) do
	if part:WaitForChild("Weight") then
		part.Name = roundDecimals(part.Position.Y, 2)
	end
end
if Print then print(script.Parent.Name.."'s Union Function End") end

(It does name the parts their Y but it's more exact if I were to check their Y property)

1 answer

✓ Accepted answer

I created this function for you.. it's pretty straightforward and basically just creates a dictionary with all the different positions inside the model with a table inside of each value naming all the parts.

function findAllPartsWithY(MODEL) 
	local t = {} -- create an empy table
	
	local function roundDecimals(num, places) -- simple rounding function
		return tonumber(string.format("%." .. (places or 0) .. "f", num))
	end
	
	for i,v in pairs(MODEL:GetChildren()) do
		if v:FindFirstChild("Weight") then
			local y = roundDecimals(v.Position.Y, 2)
			t[tostring(y)] = {}
		end
	end
	for i,v in pairs(MODEL:GetChildren()) do
		if v:FindFirstChild("Weight") then
			local y = roundDecimals(v.Position.Y, 2)
			if t[tostring(y)] then
				table.insert(t[tostring(y)], v.Name)
			end
		end
	end
	
	return t
end

findPartsWithSameY(MODEL) -- replace MODEL with whatever model you're using this on

Here is an example of it being used

Please accept my answer if this solved your problem.

Let me know if you have any questions.

  • R455

4 comments

0

Would there be a way for me to union the parts with the same Y together? I also get an error on line 8 and 26 of your script saying attempt to call a nil value

0

You need to replace MODEL inside the parenthesis with wherever your model is located in the game. In your case it would probably be script.Parent

0

You need to replace MODEL inside the parenthesis with wherever your model is located in the game. In your case it would probably be script.Parent

0

You need to replace MODEL inside the parenthesis with wherever your model is located in the game. In your case it would probably be script.Parent

This discussion is preserved as it was. New answers, comments, and votes are closed.