PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Make Players Invisible

Make Players Invisible

Jun 23 2018, 2:02 AM PST

Problem

You want to create a part which, once touched, will fade out the character who touched it and give them 10 seconds of invisibility, then you want them to fade in again.

Solution

We use debounce to prevent trying to become invisible while already invisible. We then use a table to control the fading, and iterate through the parts of the character setting parts to their appropriate transparency.

function setTransparency(char, value)
	for _, child in pairs(char:GetChildren()) do
		if child:IsA('Hat') and child:FindFirstChild("Handle") then
			child = child.Handle
		elseif child:IsA('BasePart') then
			child.Transparency = value
		end
	end
end
 
local enabled = true
Workspace.TransPart.Touched:connect(function(hit)
	local char = hit.Parent
	if char then
		local head = char:FindFirstChild("Head")
		local face = head:FindFirstChild("face")
		if enabled and head and game.Players:GetPlayerFromCharacter(char) then
			enabled = false
			for t = 0, 1, .1 do
				if face then face.Transparency = t end
				setTransparency(char, t)
				wait(0.1)
			end
			wait(10)
			for t = 1, 0, -.1 do
				if face then face.Transparency = t end
				setTransparency(char, t)
				wait(0.1)
			end
			wait(2)
			enabled = true
		end
end)

Discussion

While this code may seem long, it doesn’t have much new material. Remember in the debounce recipe we used the “enabled” variable to control the ability to activate the button. We’re going to do the exact same thing here.

While a player is invisible (and 2 seconds after they become visible), no other players can become invisible. This prevents spam invisibility and handling already invisible players in the button’s conditional.

We connect the Touched event to a part called “TransPart” which will contain this functionality. We then define 3 variables, char which is the character model, head which is the head part, and face which is an object inside the head part. The reason we cannot use multiple variable declaration statements here is that in the assignment of the new variables, we are using ones previously defined (e.g. defining the head variable uses the char variable). This face object is of the type Decal which is basically an image which is attached to a Part. This is the face of your in-game character.

In the first conditional, we check the debounce and if the object that touched “TransPart” is a player. We also make sure that this character has a head because we will manipulate the head later. We then set enabled to false to create the debounce. Here is the interesting part. We’re iterating over a table with two elements {.1, -.1}. Basically the first is the fade out, the second is the fade in. We then wait 10 seconds if we’re fading in, then set x and y to be the opposite of the original. After that, we can apply the transparency to all parts within the character as well as the face if it exists.

You’ll notice that if the object we’re currently on is a Hat, we look inside the hat and set the iteration to the handle. This is because the hats you wear have a special Hat object wrapper, to get to the actual hat itself you must index “Handle” from it. After all the iteration is done, we return the face to the character and enable the button once more.

Tags:
  • powerup
  • items
  • humanoid