Make character visible after 10 seconds

I’m messing around with an invisibility script, I want to make it so after 10 seconds, your character is visible again. I’m using wait() lines, but nothings working.

	player.CharacterAdded:connect(function() --Connects specified function when player's character is loaded.
		local character = workspace:WaitForChild(player.Name) --We can now refer to the player's character as "character".
		for i,v in pairs(character:children()) do --Gets all of character's children.
			if v:IsA("Part") or v:IsA("MeshPart") then --If a found child is a part or mesh, then
				v.Transparency = 1 --Set transparency to said child to 1
			elseif v:IsA("Accessory") then --If a found child is an accessory, then
				v:WaitForChild("Handle").Transparency = 1 --Set transparency of first child of accessory named "Handle" to 1
			end
		end
		character:WaitForChild("Head"):FindFirstChildOfClass("Decal"):Destroy() --Destroys decal in character's head
	end)
end)```
1 Like

All you have to do is copy the script you have now and replace 1s with 0s. Though the face decal is destroyed, so you’ll have to deal with that.

1 Like

Already tried that. Where would I put the copied script anyways?

You’re using a very hacky method which is using workspace:WaitForChild(player.Name). I wouldn’t recommend this because it’s just bad practice. Because of the fact that Roblox is changing the order on CharacterAdded, you won’t have to deal with this.

Read about it here:

Anyway, onto your solution.
You should be using delay and TweenService to do this, which I have provided the complete code below.

One detail I should add is that you should use GetDescendants instead of GetChildren. This should allow for you to get every part in the Character. Also, to change it back, I’m going to save each transparency in a table just incase it’s less than 0.

local TweenService = game:GetService("TweenService")

local function playerAdded(player)
	local function characterAdded(character)
		if not character:IsDescendantOf(workspace) then
			character.AncestryChanged:Wait()
		end

		local defaultTransparencies = {}

		for _, obj in ipairs(character:GetDescendants()) do
			if obj:IsA("Part") or obj:IsA("Decal") or obj:IsA("Texture") then
				defaultTransparencies[obj] = obj.Transparency
				obj.Transparency = 1
			end
		end

		delay(10, function()
			for _, obj in ipairs(character:GetDescendants()) do
				if obj:IsA("Part") or obj:IsA("Decal") or obj:IsA("Texture") then
					local tween = TweenService:Create(obj, TweenInfo.new(), { Transparency = defaultTransparencies[obj] })
					tween:Play()
				end
			end
		end)
	end

	if player.Character then
		characterAdded(player.Character)
	end

	player.CharacterAdded:Connect(characterAdded)
end

for _, player in ipairs(game.Players:GetPlayers()) do
	playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

Thanks, but your code only makes the head invisible.

Nvm, it makes thr hats still visible.

player.CharacterAdded:Connect(function() --Connects specified function when player's character is loaded.
   	local character = player.Character --We can now refer to the player's character as "character".
   	for i,v in pairs(character:GetChildren()) do --Gets all of character's children.
   		if v:IsA("Part") or v:IsA("MeshPart") then --If a found child is a part or mesh, then
   			v.Transparency = 1 --Set transparency to said child to 1
   		elseif v:IsA("Accessory") then --If a found child is an accessory, then
   			v:WaitForChild("Handle").Transparency = 1 --Set transparency of first child of accessory named "Handle" t1
   		end
   	end
   	character:WaitForChild("Head"):FindFirstChildOfClass("Decal").Transparency = 1

	wait(10)

	for i,v in pairs(character:GetChildren()) do --Gets all of character's children.
   		if v:IsA("Part") or v:IsA("MeshPart") then --If a found child is a part or mesh, then
   			v.Transparency = 0 --Set transparency to said child to 1
   		elseif v:IsA("Accessory") then --If a found child is an accessory, then
   			v:WaitForChild("Handle").Transparency = 0 --Set transparency of first child of accessory named "Handle" t1
  		end
	end
	character:WaitForChild("Head"):FindFirstChildOfClass("Decal").Transparency = 0
end)

My bad, minor mistake:

local TweenService = game:GetService("TweenService")

local function playerAdded(player)
	local function characterAdded(character)
		if not character:IsDescendantOf(workspace) then
			character.AncestryChanged:Wait()
		end

		local function descendantAdded(obj)
			if obj:IsA("Part") or obj:IsA("Decal") or obj:IsA("Texture") then
				defaultTransparencies[obj] = obj.Transparency
				obj.Transparency = 1
			end
		end

		for _, obj in ipairs(character:GetDescendants()) do
			descendantAdded(obj)
		end

		local conn = character.DescendantAdded:Connect(descendantAdded)

		delay(10, function()
			conn:Disconnect()
			for _, obj in ipairs(character:GetDescendants()) do
				if obj:IsA("BasePart") or obj:IsA("Decal") or obj:IsA("Texture") then
					local tween = TweenService:Create(obj, TweenInfo.new(), { Transparency = defaultTransparencies[obj] })
					tween:Play()
				end
			end
		end)
	end

	if player.Character then
		characterAdded(player.Character)
	end

	player.CharacterAdded:Connect(characterAdded)
end

for _, player in ipairs(game.Players:GetPlayers()) do
	playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

The hats are still visible. Other than that it’s good.

When you die, you’re still invisible.

So you use that script and make a interval:

local canfunction = true --This is the interval
if canfunction == true then
canfunction = false

And now paste your script
And now the script only works only once.If you want to make it work twice then make something like:

wait(some time)
canfunction = true

so it starts working again.

One more thing to add is that wait(10) will wait for 10+ seconds. wait() is very unpredictable and might even never resume.

It is better to use a Heartbeat loop if you want it to be precise.

local heartbeat = game:GetService("RunService").Heartbeat

local function customwait(t)
	local st = os.clock()
	while (os.clock()-st)<t do
		heartbeat:Wait()
	end
end

Yeah! I understood that he wants the player to be visible again if he dies in the game. So it the thing you mentioned is more appropriate and more likely to function better than wait() function.

That’s by design, but if you want that, then I added that

local TweenService = game:GetService("TweenService")

local function playerAdded(player)
	local function characterAdded(character)
		if not character:IsDescendantOf(workspace) then
			character.AncestryChanged:Wait()
		end

		local defaultTransparencies = {}

		local function descendantAdded(obj)
			if obj:IsA("Part") or obj:IsA("Decal") or obj:IsA("Texture") then
				defaultTransparencies[obj] = obj.Transparency
				obj.Transparency = 1
			end
		end

		for _, obj in ipairs(character:GetDescendants()) do
			descendantAdded(obj)
		end

		local conn = character.DescendantAdded:Connect(descendantAdded)

		local function makeVisible()
			conn:Disconnect()
			for _, obj in ipairs(character:GetDescendants()) do
				if obj:IsA("BasePart") or obj:IsA("Decal") or obj:IsA("Texture") then
					local tween = TweenService:Create(obj, TweenInfo.new(), { Transparency = defaultTransparencies[obj] })
					tween:Play()
				end
			end
		end

		local humanoid = character:FindFirstChildWhichIsA("Humanoid")
		humanoid.Died:Connect(makeVisible)

		delay(10, function()
			makeVisible()
		end)
	end

	if player.Character then
		characterAdded(player.Character)
	end

	player.CharacterAdded:Connect(characterAdded)
end

for _, player in ipairs(game.Players:GetPlayers()) do
	playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

Script doesn’t work, blue line under “defaulttransparencies”

Whoops, must’ve deleted the defaultTransparencies table. I don’t want to take up too many replies so I edited it above.

Instead of destroying the decal, I would do this:

character:WaitForChild("Head"):FindFirstChildOfClass("Decal").Transparency = 1

So now when you die, you’re invisible for 10 seconds. I want it to after you die, you’re not invisible anymore.

I said that I edited the reply, I’ll repost it once again:

local TweenService = game:GetService("TweenService")

local function playerAdded(player)
	local function characterAdded(character)
		if not character:IsDescendantOf(workspace) then
			character.AncestryChanged:Wait()
		end

		local defaultTransparencies = {}

		local function descendantAdded(obj)
			if obj:IsA("Part") or obj:IsA("Decal") or obj:IsA("Texture") then
				defaultTransparencies[obj] = obj.Transparency
				obj.Transparency = 1
			end
		end

		for _, obj in ipairs(character:GetDescendants()) do
			descendantAdded(obj)
		end

		local conn = character.DescendantAdded:Connect(descendantAdded)

		local function makeVisible()
			conn:Disconnect()
			for _, obj in ipairs(character:GetDescendants()) do
				if obj:IsA("BasePart") or obj:IsA("Decal") or obj:IsA("Texture") then
					local tween = TweenService:Create(obj, TweenInfo.new(), { Transparency = defaultTransparencies[obj] })
					tween:Play()
				end
			end
		end

		local humanoid = character:FindFirstChildWhichIsA("Humanoid")
		humanoid.Died:Connect(makeVisible)

		delay(10, function()
			makeVisible()
		end)
	end

	if player.Character then
		characterAdded(player.Character)
	end

	player.CharacterAdded:Connect(characterAdded)
end

for _, player in ipairs(game.Players:GetPlayers()) do
	playerAdded(player)
end

game.Players.PlayerAdded:Connect(playerAdded)

I know, I am using it. The same thing is happening.