[Solved by Xaplize]How to make a Player Visible/Invisible button

Hello Iam Sam
So today iam trying to make a gui button that when u click it all other player in the game being invisible or almost invisible and click a again to be visible again
(This only show to you other still see as usual)
Idea inspired by Flood Escape 2

And how do i make that ?
Any help is appriciate thanks alot !

1 Like

Do you want to make it so that only the person that pressed the button sees the effect or it affects other people?

If you only want it to appear on one client, you can just do the whole code in a local script, iterating every single player’s character and make each BasePart instance in their model transparent, the same code logic goes to affecting other players’ views except you should do the code on the server.

1 Like

Simply gather all the parts of the local player’s character, and make their transparency go to 1 for full invisibility. This would go inside of the UI you are wanting them to be able to click, or in a local script inside of StarterPlayerScripts or StarterCharacterScripts based on your game. Note that this is only an example, you may have to change the variables a bit to fit your gui.

local GUI = script.Parent
local Button = GUI:WaitForChild("TextButton")
local Invisibility = false
local Character = game.Players.LocalPlayer.Character

Button.MouseButton1Click:Connect(function() -- Connects a function when the button is clicked
Invisibility = not Invisibility
if Invisibility then

for i,v in pairs(Character:GetChildren()) do -- Gathers all the parts of the character that is a basepart
if v:IsA("BasePart") then
v.Transparency = 1 -- Makes them invisible
   end
end

else

for i,v in pairs(Character:GetChildren()) do
if v:IsA("BasePart") then
v.Transparency = 0 -- They are no longer invisibile
   end
end
end)

ty u helped alot
But maybe i want that the player doesnt see other player.Character after click the button

Like clearing the way so the other player.character doesnt block their sight in some situation

Its easy, just use player.Character:GetChildren() and check whether its a meshpart or basepart, if it is one of them, then set the transparency to 1

Invisible Other Players On Client Guide


If you want to invisible other people on a client - the code below will do the job!

Code 1 (Open)
local Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) do
    if player.Character and player ~= Players.LocalPlayer then
        player.Character.Parent = nil
    end
end

When ran, the code above will make everyone except for you invisible as soon as your character spawned.

HEY!!!

That doesn’t answer my question! I need a button that you can activate to hide other players, not a bunch of lines thrown to me and let me to figure out. Be patient, we will solve the code’s logic one by one before finishing the actual product:


Question (And answers, obviously)!


What is player.Character and player ~= Players.LocalPlayer, exactly?
Let’s break down them into two part of logics: player.Character and player ~= Players.LocalPlayer.

Logic 1

First, what is player.Character? You could imagine a dot stands for 's, which player.Character could mean player’s Character. But why would you add a if statement to check that?? Sometimes, if the player’s character is in the void, you will get no returns since how are you supposed to get a destroyed object without magic? So that’s why we use monday.com if statements to check does it exist or not.


Logic 2

Second, what is player ~= Players.LocalPlayer? Players.LocalPlayer is the client that ran the script, which is yourself, so the above statement will check if the player value is not same (~=) with the player that ran the script, if it’s really yourself, don’t invisible it and vice-versa.



What is player.Character.Parent = nil?
The code will parent the player.Character to nil so the player will never respawn on the client. If you made a game that is intended to have collisions between each other and you want this feature, this tutorial may not be suitable for you because parenting the player’s character to nil will not make the player character be in the playable bounds (workspace) so the client will not see the other character and will collide with each other. Either way, you shouldn’t make an invisible player feature in that situation.


Why not destroy the characters so they are “invisible”?
There’s not much explanation but if you destroy the characters you are never going to get the characters back to visible


BACK TO THE TOPIC

Aside from the big text, how to make them back to visible? Huh. You can use the code above but instead of player.Character.Parent = nil, we parent it to workspace (the playable bounds).

If you want to visible/invisible other people through a button, you will need a MouseButton1Click signal to check if it’s clicked or not and a variable for turning it on and off to toggle:

(First, we will set up the MouseButton1Click signal and we will toggle it on to off or off to on (like a light switch) after activating the button clicked signal, and then we will check if the variable is on or off, if it’s off then invisible everyone excluding the client (player that clicked the button), if it’s on just make everyone visible)

Final code (Open)
--\\ Services
local Players = game:GetService("Players")

--\\ Scopes
local Button = script.Parent
local PlayerHidden = false

--\\ Functions
local function ButtonPressed()
    PlayerHidden = not PlayerHidden
    -- not true will results false; not false will results true
    -- it basically reverses it (PlayerHidden variable)

    if PlayerHidden then
        for _, player in pairs(Players:GetPlayers()) do
            if player.Character and player ~= Players.LocalPlayer then
                player.Character.Parent = nil
            end
        end
    else
        for _, player in pairs(Players:GetPlayers()) do
            if player.Character and player ~= Players.LocalPlayer then
                player.Character.Parent = workspace
            end
        end
    end
end

--\\ Connections
Button.MouseButton1Click:Connect(ButtonPressed)

The explorer tree will be like this to have the final code works:

Screen Shot 2021-11-14 at 9.06.43 PM

or

Screen Shot 2021-11-14 at 9.08.03 PM

or whatever! As long as the script is parented inside the button you want the player to press for the player to be visible/invisible

If you followed the steps and read through them carefully you might understand the concept, if not maybe you have to read again or reply me!

The script will not be 100% efficient or 100% correct or the best way possible, but it’s a method that is clean

(I won’t explain everything one by one because this post will be too long, you might need some coding basis if you want to understand what’s going on from the script or what alien language I am speaking as I already explained the concept and the rest is just setups or stuffs)

alright bye :wave:

5 Likes

got it appriciate all helped me
Ty all

el problema de esto es que cuando lo vuelvas a activar, si el usuario se acababa de mover y esta afk realmente aparecerá en la misma posición que lo desactivastes, entoces el usuario se tiene que mover para que aparezca en su posicion actual. Por esto es mejor no destruirlas que si hacerlo.(no se si me ha entendido)