Turning all players invisible and then back to normal again

I am trying to create a button that turns all players invisible on your screen and then returning them back to normal, as they were before hand. This is very simple and I could do the first part pretty easily.

However, the issue is returning characters to their original state. If the player’s hat were not completely opaque (such as the Ice Egg from the recent 2020 Egg Hunt, which has a part transparency of 0.35) then the hat would return to 0 transparency alongside everything else, which is not what I want.

How can I save each hat’s transparency so that they most return to their original transparency without creating tons of needless variables?

2 Likes

Well, actually it’s easy to make, just insert some important proprieties of each part of each player character to a table, then change their proprieties and when you want to put them back, just loop in the table and you made it.
For the table structure you can do this:

local accessories = {}

--saving players' accessories
for a,b in pairs(game.Players:GetChildren()) do
    local ch = b.Character or b.CharacterAdded:Wait()
    accessories[b.Name] = {}
    
    for c,d in pairs(ch:GetChildren()) do
        if d:IsA("Part") then
            accessories[b.Name][d.Name] = {}
            
            accessories[b.Name][d.Name]["Transparency"] = d.Transparency
            accessories[b.Name][d.Name]["Reflectance"] = d.Reflectance
            accessories[b.Name][d.Name]["Parent"] = nil
            accessories[b.Name][d.Name]["Name"] = d.Name
        elseif d:IsA("Accessory") then
            local handle = d["Handle"]

            accessories[b.Name][d.Name] = {}

            accessories[b.Name][d.Name]["Transparency"] = handle.Transparency
            accessories[b.Name][d.Name]["Reflectance"] = handle.Reflectance
            accessories[b.Name][d.Name]["Parent"] = d.Name
            accessories[b.Name][d.Name]["Name"] = handle.Name
        end
    end
end

--changing their proprities
for a,b in pairs(accessories) do
    local plr = game.Players:FindFirstChild(tostring(b))
    local ch = plr.Character or plr.CharacterAdded:Wait()
    
    if plr then
        for c,d in pairs(accessories[plr.Name]) do
            local parent = d["Parent"]
            if parent ~= nil then
                local part = ch[parent][d["Name"]]
                if part then
                    part.Transparency = 1
                    part.Reflectance= 0
                end
            end
        end
    end
end

wait(3) --just to separate

--changing them back
for a,b in pairs(accessories) do
    local plr = game.Players:FindFirstChild(tostring(b))
    local ch = plr.Character or plr.CharacterAdded:Wait()
    
    if plr then
        for c,d in pairs(accessories[plr.Name]) do
            local parent = d["Parent"]
            if parent ~= nil then
                local part = ch[parent][d["Name"]]
                if part then
                    part.Transparency = d["Transparency"]
                    part.Reflectance= d["Reflectance"]
                end
            end
        end
    end
end

Im tired becouse i live in italy and here its sleep time, so i think i made some mistakes, anyways i hope this works. :slightly_smiling_face:

2 Likes
local accessoryData = {}

for _, v in pairs(game.Players:GetChildren()) do 
    repeat wait() until v.Character
    local character = v.Character
    for _, k in pairs(character:GetChildren())
        if k:IsA("Accessory") then
            if k.Transparency ~= 0 then
                table.insert(accessoryData,1,k)
            end
            k.Transparency = 1
        end
    end
end

wait()

for _, v in pairs(game.Players:GetChildren()) do
    repeat wait() until v.Character
    local character = v.Character
    for _, k in pairs(character:GetChildren())
        if k:IsA("Accessory") then
            if not accessoryData.k then
                k.Transparency = 0
            else
                k.Transparency = accessoryData.k.Transparency
            end
        end
    end
end
1 Like

Accessories do not have Transparency values, so I changed every mention of k.Transparency to k.Handle.Transparency.

Also, this script worked well, but transparent hats still become opaque when the script bounces back.

Thank you for the help either way! I will see how I can fix it.

2 Likes

Sorry about that I hope it works well

You can loop through the player parts from a local script and do something like:

for _,char in ipairs(Players:GetChildren()) do
    for _,part in ipairs(char:GetDescendants()) do
        if part.ClassName == "Part" or part.ClassName == "MeshPart" then
            part.Transparency = part.Transparency==1 and 0 or 1
        end
    end
end

It basically loops through all the players, then it loops through all the character parts and checks if the part is of type Part to then change the Transparency, if the transparency is 1 then return 0 otherwise return 1.