How to make player completely invisible when they press e

I want to know how to make a player completely invisible when they press e and after 3 seconds they become visible again.

2 Likes

make a local script to listen for players pressing the E button – I’d use ContextActionService

make a remote event to let the server know that the button was pressed

On the server script when remote event is received, loop through the character and make everything desired transparent. Store the existing transparencies inside a table, setup a wait timer for 3 seconds and loop back through the table of stored transparencies and restore onto the character parts

3 Likes

and thats what i want to know
how to get the player parts without a for loop

You should learn how to code.

https://developer.roblox.com/en-us/api-reference/class/UserInputService

2 Likes

Why not? Without this, you’d need to index each individual part which isnt efficient when you can just do the same exact thing in 4 lines of code??

1 Like

Well, you kinda have to use a for loop. You could pre-specify all the parts in a table, but players can have accessories that your script wouldn’t account for, but a for loop could take care of it

1 Like

you cant, its pretty much required or you can do

leg.transparency = 1
arm.transparency = 1

but that just gives messy code

1 Like

will the for loop take longer than the example you shown ?
if so then ill make something similar to what you did but if the for loop is shorter i will use a for loop.So what i want to know is if the for loop is longer or shorter

nope, its a way faster way of doing what i said above

1 Like
local a = {}
for _, parts in pairs(Character:GetDescendants()) do
    a[parts] = parts.Transparency
    parts.Transparency = 1
end
wait(3)
for part, transparency in pairs(a) do
    part.Transparency = transparency
end

This is much cleaner looking and takes much less time to write out than each part to set the transparency to invisible, then waiting, and doing it again to set it back to w/e you wanted it to be
Edited because i messed up some variable naming

3 Likes

You could’ve directly just set it’s transparency without adding a table?? Why did you add that a table?

The reason for the table is because he wanted to set his transparency back after 3 seconds, you could go through and just set everything back to 0, but some things are hidden intentionally, like the HumanoidRootPart, and if he had anything else this would help account for it.

You could put in if statements inside the for loop to search for the specific parts you don’t want to unhide, but this makes sure that whatever things were set to, they get set back to that same property.

dude that’s ez pz.

for _,part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.Transparency = 1
end
end
task.wait(3)
for _,part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.Transparency = 0
end
end

Yes, this would work, like I said you could just do that, but this would make the HumanoidRootPart show, and this is not a part that should normally show, and as before, you can just specify in the second for loop to check if the part is a humanoidrootpart, but if things change, and you don’t want to have to make changes in the future, the solution i provided would be able to check each part’s transparency, save that data, then restore it back to what i was, maybe they decide to have something else hidden on the character, say the player was holding a tool and it had hidden parts, your code would just unhide those parts, and you would have to specify each part to not be unhidden, and this would be problematic because not every tool would have the same parts

Then simply ignore the humanoid root part.

for _,part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.Transparency = 1
end
end
task.wait(3)
for _,part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
if part.Name == "HumanoidRootPart" then else
part.Transparency = 0
end
end
end

Adding another table is unnessecary tbh.

for goodness sakes dude, format your code.

Roblox studio should auto do this for you.

See my edit regarding equipping tools/items.

I don’t feel it’s unnecessary, but that’s just how I feel =) you can feel that it’s unnecessary.

I like how my version requires much less lines of code, no if statements, it just works properly and sets things back to the way they were. If this is not the option you want to utilize, then you don’t have to

M, I’m on phone rn so it’s hardly formatted.

--LOCAL

local userInput = game:GetService"UserInputService"
local replicated = game:GetService"ReplicatedStorage"
local remoteEvent = replicated:WaitForChild"RemoteEvent"

userInput.InputBegan:Connect(function(inputObject, wasProcessed)
	if wasProcessed then return end

	if inputObject.KeyCode == Enum.KeyCode.E then
		remoteEvent:FireServer()
	end
end)
--SERVER

local replicated = game:GetService"ReplicatedStorage"
local remoteEvent = replicated.RemoteEvent

local function visibilityToggleRecursive(object, bool)
	for _, child in ipairs(object:GetChildren()) do
		if child:IsA"BasePart" then
			if child.Name ~= "HumanoidRootPart" then
				child.Transparency = if bool then 1 else 0
			end
		elseif child:IsA"Accessory" then
			visibilityToggleRecursive(child, bool)
		elseif child:IsA"Tool" then
			visibilityToggleRecursive(child, bool)
		end
	end
end

remoteEvent.OnServerEvent:Connect(function(player)
	local character = player.Character
	if not character then return end
	visibilityToggleRecursive(character, true)
	task.wait(3)
	visibilityToggleRecursive(character, false)
end)
4 Likes