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)```
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)
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)
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)
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)
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)