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

Randomly iterate through parts of model, until entire model is transparent?

Asked by 7 years ago
Edited 7 years ago

I'm trying to make a model fade with a "flicker-ish" effect by randomly iterating through each part of the model, then adding transparency, and repeating this process until ALL children of the model are transparent.

The script I have is kindof working. The problem I'm running into is that from time-to-time, not ALL of the parts in the model are made transparent, but the script ends anyways. So sometimes there will be 1, 2, 3, 4 or more parts that are still kinda visible although the function is finished. Why is this happening?

I've included the function below. If I can clarify anything further, please let me know.

Variables to know:

Fields = a table of parts in the model

randHit = a local table that I use to cross-check what parts have been fully modified or not (this step might be unnecessary, I use this check to eliminate further transparency checks since I know the part is already fully transparent)

hit = a local bool that I set to true IF the part being checked is already inside of randHit

allTrans = a bool that I use to fully check if ALL parts of the model are transparent. Is set to true each time the loop activates and then is set to false ANY time one of the parts isn't transparent enough. Loop only finishes if allTrans is still true by the end

function onFunction()
    repeat 
        wait(0)
        allTrans = true
        local numHit = 0
        local randHit = {}
        local hit = false

        repeat
            hit = false
            local inc = math.random(1,#Fields) 
            for i, v in pairs (randHit)do -- this loop might be unnecessary
                if Fields[inc] == v then 
                    hit = true
                end
            end

            if not hit then
                if Fields[inc].Transparency > transAmt then
                    Fields[inc].Transparency = Fields[inc].Transparency - .2
                    allTrans = false
                else
                    table.insert(randHit,Fields[inc])
                end

            end

            numHit = numHit + 1

        until numHit == (#Fields)
    --[[    
        for i, v in pairs(Fields) do
            wait(0)
            allTrans = true
            if v.Transparency > transAmt then
                allTrans = false
                v.Transparency = v.Transparency - .3
            end
    end]]

    until allTrans == true


    for _,v in pairs(script.Parent.buttons:GetChildren()) do
        if v:IsA("BasePart") then
            v.BrickColor = BrickColor.new("Lime green")
        end
    end
    on = true   
    debounce = false
end

Answer this question