Pretty sure it's because you're looping through every player twice (with two different forms of loops, might I add.) Changing this to one loop and fixing up the formatting and disorganization of the code so it is no longer comparable to my little brother's bedroom fixes this. Here is the final product that I came up with that worked when I tested it (still in a LocalScript):
01 | local invisible = false |
02 | local players = game:GetService( "Players" ):GetPlayers() |
03 | local localPlayer = game:GetService( "Players" ).LocalPlayer |
05 | script.Parent.Activated:Connect( function () |
06 | for _, player in pairs (game.Players:GetChildren()) do |
07 | local char = player.Character |
08 | if not invisible and player ~ = localPlayer then |
09 | for _, part in pairs (char:GetChildren()) do |
10 | if part:IsA( "BasePart" ) and part.Name ~ = "HumanoidRootPart" then |
12 | elseif part:IsA( "Accessory" ) then |
13 | part.Handle.Transparency = 1 |
17 | elseif invisible and player ~ = localPlayer then |
18 | for _, part in pairs (char:GetChildren()) do |
19 | if part:IsA( "BasePart" ) and part.Name ~ = "HumanoidRootPart" then |
21 | elseif part:IsA( "Accessory" ) then |
22 | part.Handle.Transparency = 0 |
It's been shortened quite a bit and is also much more comprehensible, but it's understandable if you don't understand something because it is not written in the same way that you write your code. So, if it works (which is good) but you don't understand part of the code, please let me know so I can clear up any confusion as I do not want you to use code you cannot even understand and learn anything from. Also, please let me know if it doesn't work at all so I can help fix that as well. Good luck!