Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
for i,Child in pairs(char:GetChildren()) do
if Child:IsA('Part') or Child:IsA('MeshPart') then
Child.Transparency = 1
end
end
wait(1.25)
for i,Child in pairs(char:GetChildren()) do
if Child:IsA('Accessory') then
Child:Destroy()
end
end
end)
end)

2 answers

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

local UIS = game:GetService("UserInputService") -- Get user input service
local Visible = true
local player = game.Players.LocalPlayer
UIS.InputBegan:Connect(function(key) -- when player presses a key
if key.KeyCode == Enum.KeyCode.R then -- Change R to the key you want
	if Visible == true then
game.ReplicatedStorage.RemoteEvent1:FireServer() -- We have to fire server
Visible = false
	elseif Visible == false then
		game.ReplicatedStorage.RemoteEvent2:FireServer()
		Visible = true
	end
end
end)

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

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

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

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

I've tested this out and it worked, enjoy

3 comments

0

v:IsA("BasePart") will take all the part types

0

and you don't need two remote events

0

He used meshpart and part in the original so I wasnt sure he wanted everything invisible

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

localScript in startercharacterScripts

local player = game.Players.LocalPlayer
local character = script.Parent -- Since the script will be inside the character, easier way is to use script.Parent
repeat wait() until character:FindFirstChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local isInvisible = false

UserInputService.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
	if input.KeyCode == Enum.KeyCode.L  then-- Your button for making the character invisible
		game:GetService("ReplicatedStorage").RemoteEvent:FireServer(isInvisible)
	end
end)

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")

RemoteEvent.OnServerEvent:Connect(function(player,isInvisible) -- In Server Event, the first parameter is always the player
	if not isInvisible then
		isInvisible = true
		for _,v in pairs(player.Character:GetDescendants()) do -- Descendants are ALL the parts including the children of the children
			if v:IsA("BasePart") or v:IsA("Decal") then -- Because parts and decals both have transparency, this will make the script shorter
				v.Transparency = 1
			elseif v:IsA("Accessory") then
				v:FindFirstChild("Handle").Transparency = 1
			end
		end
	elseif isInvisible then
		isInvisible = false
		for _,v in pairs(player.Character:GetDescendants()) do
			if v:IsA("BasePart") or v:IsA("Decal") then
				v.Transparency = 0
			elseif v:IsA("Accessory") then
				v:FindFirstChild("Handle").Transparency = 0
			end
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.