Hello! I wanted to make something that makes others invisible when a GUI gets pressed. The problem is, Accesories don’t have a Transparency property, so I have to destroy them.
I want it so that if they press it again, they can see other players again. My script looks like this: It works fine but it destroys the accesories so you can’t make them visible again. It’s local, of course.
So is there a possibility to just make them transparent, somehow?
local plr = game.Players.LocalPlayer
local playerGui = plr:WaitForChild("PlayerGui")
local ScreenGui = playerGui:FindFirstChild("ScreenGui") or playerGui:WaitForChild("ScreenGui")
local TextButton = ScreenGui.TextButton
local function MakePlrsInvisible()
for _, player in pairs(game.Players:GetChildren()) do
if player.Name ~= plr.Name then
local playerName = player.Name
for _, char in pairs(game.Workspace:GetChildren()) do
if char.Name == playerName then
char:FindFirstChild("Head").face:Destroy()
for _, BodyPart in pairs(char:GetChildren()) do
if BodyPart:IsA("Part") or BodyPart:IsA("MeshPart") then
BodyPart.Transparency = 1
elseif BodyPart:IsA("Accessory") then
BodyPart:Destroy()
end
end
end
end
end
end
end
TextButton.MouseButton1Click:Connect(MakePlrsInvisible)
Accessories do not have a Transparency property, but the handles of them do.
Try looking for the accessory’s handle with FindFirstChild, then setting the transparency to 1/0 respectively.
Another option is to just use GetDescendants so you can get the items of a character in a one-dimensional array and then just work off of that. This is much easier than having to set up multiple loops or checks to find all the parts of a character. Just handle it from there.
Your code is also doing some extra work that you should cut down on. Don’t iterate over all items of the Workspace per player, you will generate performance problems. You’re already iterating over all the players so just access their character and work from there.
-- Use ipairs for arrays, not pairs. Use GetService as the canonical way to
-- fetch a service. Finally, use GetPlayers, not GetChildren.
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
-- Do a referential comparison, no need to compare by name. Skip the
-- iteration if the references are the same.
if player == plr then continue end
-- Skip iteration if they don't have a character, return will cancel loop
if not player.Character then continue end
-- Handle all the descendants of the character
for _, object in ipairs(player.Character:GetDescendants()) do
-- Parts have a superclass called BasePart, use this instead of
-- checking over several classes.
if object:IsA("BasePart") or object:IsA("Decal") then
-- Use a multiplier, this is better than hard setting transparency.
-- Yes, decals also have LTM.
object.LocalTransparencyModifier = 1
end
end
end
Basically, your entire MakePlrsInvisible function, but better on performance by improving the efficiency of what its doing using variables we already have access to.