I am trying to make a script where when a text button gets clicked it makes every player ingame except for local player invisible, but it only seems to make one player invisible.
script.Parent.MouseButton1Click:Connect(function() lol = true p = game.Players:GetPlayers() for i = #p, 0 ,-1 do for i,v in pairs(game.Players:GetChildren()) do local char = v.Character if char.Head.Transparency == 0 and lol == true and v.Name ~= game.Players.LocalPlayer.Name then char.Head.Transparency = 1 char.Torso.Transparency = 1 char["Left Arm"].Transparency = 1 char["Left Leg"].Transparency = 1 char["Right Arm"].Transparency = 1 char["Right Leg"].Transparency = 1 char.Head.face.Transparency = 1 lol = false for i,v in pairs(char:GetChildren()) do if v.ClassName == "Accessory" then v.Handle.Transparency = 1 end end end if char.Head.Transparency == 1 and lol == true then char.Head.Transparency = 0 char.Torso.Transparency = 0 char["Left Arm"].Transparency = 0 char["Left Leg"].Transparency = 0 char["Right Arm"].Transparency = 0 char["Right Leg"].Transparency = 0 char.Head.face.Transparency = 0 lol = false for i,v in pairs(char:GetChildren()) do if v.ClassName == "Accessory" then v.Handle.Transparency = 0 end end end end end end)
this is the error:
https://gyazo.com/f41049c8234959ca950a6e825707102b
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):
local invisible = false local players = game:GetService("Players"):GetPlayers() local localPlayer = game:GetService("Players").LocalPlayer script.Parent.Activated:Connect(function() for _, player in pairs(game.Players:GetChildren()) do local char = player.Character if not invisible and player ~= localPlayer then for _, part in pairs(char:GetChildren()) do if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then part.Transparency = 1 elseif part:IsA("Accessory") then part.Handle.Transparency = 1 end end invisible = true elseif invisible and player ~= localPlayer then for _, part in pairs(char:GetChildren()) do if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then part.Transparency = 0 elseif part:IsA("Accessory") then part.Handle.Transparency = 0 end end invisible = false end end end)
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!
this script should work and is filtering enabled compatible which is what you should be going for
--local script in starter Gui
local remoteEvent = game.ReplicatedStorage.RemoteEvent local textButton = script.Parent textButton.MouseButton1Click:Connect(function() remoteEvent:FireServer() end) --script in Server script service game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function() for i,v in pairs (game:GetPlayers()) do if v:IsA("BasePart") and v.Parent.FindFirstChild("Humanoid") then v.Transparency = 0 end end end)