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

how do I make it so when text button gets clicked all players go invisible?

Asked by
zValerian 108
5 years ago

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.

01script.Parent.MouseButton1Click:Connect(function()
02    lol = true
03 
04    p = game.Players:GetPlayers()
05 
06 
07for i = #p, 0 ,-1 do   
08for i,v in pairs(game.Players:GetChildren()) do
09 
10 
11 
12      local char = v.Character
13 
14if char.Head.Transparency == 0 and lol == true and v.Name ~= game.Players.LocalPlayer.Name then
15    char.Head.Transparency = 1
View all 50 lines...

this is the error:

https://gyazo.com/f41049c8234959ca950a6e825707102b

0
is this in a script? Mr_Pure 129 — 5y
0
local script, its a toggle zValerian 108 — 5y

2 answers

Log in to vote
0
Answered by
lunatic5 409 Moderation Voter
5 years ago

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):

01local invisible = false
02local players = game:GetService("Players"):GetPlayers()
03local localPlayer = game:GetService("Players").LocalPlayer
04 
05script.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
11                    part.Transparency = 1
12                elseif part:IsA("Accessory") then
13                    part.Handle.Transparency = 1
14                end
15            end
View all 28 lines...

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!

Ad
Log in to vote
0
Answered by 5 years ago

this script should work and is filtering enabled compatible which is what you should be going for

--local script in starter Gui

01local remoteEvent = game.ReplicatedStorage.RemoteEvent
02local textButton = script.Parent
03textButton.MouseButton1Click:Connect(function()
04       remoteEvent:FireServer()
05end)
06 
07--script in Server script service
08game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
09    for i,v in pairs (game:GetPlayers()) do
10            if v:IsA("BasePart") and v.Parent.FindFirstChild("Humanoid") then
11                v.Transparency = 0
12                end
13    end
14end)

Answer this question