Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
1
Answered by
Wiscript 622 Moderation Voter
4 years ago

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
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` ArvidSilverlock 50 — 4y
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 Wiscript 622 — 4y
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 Wiscript 622 — 4y
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 Wiscript 622 — 4y
Ad

Answer this question