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

How to change transparency of everything inside?

Asked by 3 years ago
Edited 3 years ago

How can I modify this script in order for everything inside "Pieces" to change Transparency, when the Transparency inside "Bags" are all zero. There are six bags in total inside the "Bags" model, they don't change transparency altogether as it requires the player to get in contact with the bags.

local bags = script.Parent.Bags:GetChildren()
local pieces = script.Parent.Pieces:GetChildren()

for i,bags in pairs(bags) do
    if bags.Transparency == 0 then
        print("all bags are visible...")
        for i,pieces in pairs(pieces) do
            pieces.Transparency = 0
            print("puzzle pieces are visible...")
        end
    end
end
0
My issue is that, the pieces don't change transparency at all even though the transparency inside "Bags" are all 0. fleurpetaIs 0 — 3y

1 answer

Log in to vote
0
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

well, first off, and I’m not sure if this has an effect on the script or not, there’s no reason to use the same variable in the iterator and the loop variable. notice how you’re writing bags in pairs(bags), why not bag in pairs(bags)? same problem with the second loop. you are overwriting the same variable which could have unintended side effects, and be breaking your script (not 100% certain right now if that’s the issue however).

next thing, I take it you’re checking every bag for transparency at once. well on one line you wrote “all bags are visible” after checking only the first bag. there’s no good in that, not to mention it will run six times! you have to check all the bags and then make a decision.

function checkTransparency(instances)
    for _, instance in pairs(instances) do
        if instance.Transparency ~= 0 .then
            return false
        end
    end
    return true
end

if checkTransparency(bags) then
    for _, piece in pairs(pieces) do
        piece.Transparency = 0
    end
end

the function checkTransparency checks every bag and returns false if it finds a bag that is transparent. it only returns true if every bag passed the test. only then, can you set the transparency of the pieces.

Ad

Answer this question