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

How to make Player Invisible Then Visible When pressing a key 2 times?

Asked by 5 years ago

Basically, a script that makes the player invisible by pressing a key but, pressing it again makes the character visible?

how would i turn this code into that?

01game.Players.PlayerAdded:Connect(function(plr)
02plr.CharacterAdded:Connect(function(char)
03for i,Child in pairs(char:GetChildren()) do
04if Child:IsA('Part') or Child:IsA('MeshPart') then
05Child.Transparency = 1
06end
07end
08wait(1.25)
09for i,Child in pairs(char:GetChildren()) do
10if Child:IsA('Accessory') then
11Child:Destroy()
12end
13end
14end)
15end)

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

First of all, Make a local script and put it in StarterPack or StarterPlayerScripts

01local UIS = game:GetService("UserInputService") -- Get user input service
02local Visible = true
03local player = game.Players.LocalPlayer
04UIS.InputBegan:Connect(function(key) -- when player presses a key
05if key.KeyCode == Enum.KeyCode.R then -- Change R to the key you want
06    if Visible == true then
07game.ReplicatedStorage.RemoteEvent1:FireServer() -- We have to fire server
08Visible = false
09    elseif Visible == false then
10        game.ReplicatedStorage.RemoteEvent2:FireServer()
11        Visible = true
12    end
13end
14end)

You need to make 2 RemoteEvents. Put them both in ReplicatedStorage. Name the first "RemoteEvent1" And the second "RemoteEvent2"

Now create a server script ("script") and put it in ServerScriptService

1game.ReplicatedStorage.RemoteEvent1.OnServerEvent:Connect(function(plr)
2    for i,v in pairs(game.Workspace[plr.Name]:GetDescendants()) do
3        if v:IsA("Part") or v:IsA("MeshPart") then
4            v.Transparency = 1 -- this makes them invisible
5        end
6    end
7end)

Then make another server script and put it in the same place. Make the code for that script

1game.ReplicatedStorage.RemoteEvent2.OnServerEvent:Connect(function(plr)
2    for i,v in pairs(game.Workspace[plr.Name]:GetDescendants()) do
3        if v:IsA("Part") or v:IsA("MeshPart") then
4            v.Transparency = 0
5        end
6    end
7end)

I've tested this out and it worked, enjoy

0
v:IsA("BasePart") will take all the part types AnasBahauddin1978 715 — 5y
0
and you don't need two remote events AnasBahauddin1978 715 — 5y
0
He used meshpart and part in the original so I wasnt sure he wanted everything invisible iiConstable_Subwayx 130 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

you have to use userinputService. You also need a remote event if you want everyone to see your character invisible,

localScript in startercharacterScripts

01local player = game.Players.LocalPlayer
02local character = script.Parent -- Since the script will be inside the character, easier way is to use script.Parent
03repeat wait() until character:FindFirstChild("Humanoid")
04local UserInputService = game:GetService("UserInputService")
05local isInvisible = false
06 
07UserInputService.InputBegan:Connect(function(input,gameProcessedEvent) -- input is the button the player pressed and gameProcessedEvent makes sure that you are not chatting, since if you dont use it players will accidentally press a button while chatting
08    if input.KeyCode == Enum.KeyCode.L  then-- Your button for making the character invisible
09        game:GetService("ReplicatedStorage").RemoteEvent:FireServer(isInvisible)
10    end
11end)

Server Script:

01local ReplicatedStorage = game:GetService("ReplicatedStorage")
02local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
03 
04RemoteEvent.OnServerEvent:Connect(function(player,isInvisible) -- In Server Event, the first parameter is always the player
05    if not isInvisible then
06        isInvisible = true
07        for _,v in pairs(player.Character:GetDescendants()) do -- Descendants are ALL the parts including the children of the children
08            if v:IsA("BasePart") or v:IsA("Decal") then -- Because parts and decals both have transparency, this will make the script shorter
09                v.Transparency = 1
10            elseif v:IsA("Accessory") then
11                v:FindFirstChild("Handle").Transparency = 1
12            end
13        end
14    elseif isInvisible then
15        isInvisible = false
View all 24 lines...

Answer this question