Is there a way to check how much parts in model are unanchored

My goal is to check every second (or collision) how much parts in model are unanchored. I need it for car destruction mechanic. Car already breaks but i’m trying to add smoke once it’s heavily damaged.

i used the “unanchored” state as an example. Im not really using anchored/unanchored in my destruction physics.

Im trying to avoid the loops, but can’t figure out how to make my script work without them. im currently working on a destruction physics game as my first project, and i’m stuck with a car, and as you know it’s already a lot of scripts, loops and parts when it comes to this genre.

I can do it with some loops, of course, but i don’t really think it’s necessary and there’s another way of doing it.

1 Like

Correct me if I’m wrong but you would need at least 1 loop.

What you could do is connect each part in the loop with the .Changed event and wait till the changed property was the Anchored property. Implement a counter and a threshold, when the counter exceeds the threshold, you can add smoke. Here’s an example:

local model = script.Parent -- Adjust this to reference your model
local unanchoredCount = 0
local smokeThreshold = 5 -- Adjust this threshold based on your needs

-- Function to update unanchored count and check for smoke effect
local function updateUnanchoredCount(part, isUnanchored)
    if isUnanchored then
        unanchoredCount = unanchoredCount + 1
    else
        unanchoredCount = unanchoredCount - 1
    end

    -- Trigger smoke effect if the count exceeds the threshold
    if unanchoredCount >= smokeThreshold then
        -- Add your smoke effect triggering code here
    end
end

-- Function to initialize part tracking
local function trackPart(part)
    if part:IsA("BasePart") then
        part.Changed:Connect(function(property)
            if property == "Anchored" then
                updateUnanchoredCount(part, not part.Anchored)
            end
        end)
        -- Initialize counter based on current state
        if not part.Anchored then
            unanchoredCount = unanchoredCount + 1
        end
    end
end

-- Track all parts in the model
for _, part in pairs(model:GetDescendants()) do
    trackPart(part)
end
3 Likes

Thanks, that really helped me. I didn’t even think about counting parts once they changed. And i think there would be at least one loop anyways, i just wanted to avoid loop spam, not exclude them at all

1 Like

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