I recently wrote this script for a Death-Screen animation to play when the player dies/resets their character. It works okay, however it only works once. The first time the player resets, the Tweens will play, however they will not play again after that.
The Script:
local player = game.Players.LocalPlayer
local Fade = script.Parent.Frame
player.Character:WaitForChild('Humanoid').Died:Connect(function()
Fade.Visible = true
--The first tween
local Info = TweenInfo.new(1)
local Tween = game:GetService("TweenService"):Create(Fade,Info,{BackgroundTransparency=0})
Tween:Play()
Tween.Completed:Wait()
wait(2)
--The second Tween
local Info2 = TweenInfo.new(1)
local Tween2 = game:GetService("TweenService"):Create(Fade,Info2,{BackgroundTransparency=1})
Tween2:Play()
Tween2.Completed:Wait()
wait(2)
Fade.Visible = false
end)
Where is this script located in? If it’s somewhere where it can’t detect the new character, such as StarterPlayerScripts or in a ScreenGui with ResetOnSpawn disabled, it cannot detect the new character when you respawn, it needs to be somewhere where the script is replicated as well
local player = game.Players.LocalPlayer
local Fade = script.Parent.Frame
player.Character:WaitForChild('Humanoid').Died:Connect(function()
--The second Tween
local Info2 = TweenInfo.new(1)
local Tween2 = game:GetService("TweenService"):Create(Fade,Info2,{BackgroundTransparency=1})
Tween2:Play()
Tween2.Completed:Wait()
Fade.Visible = false
wait(2)
Fade.Visible = true
--The first tween
local Info = TweenInfo.new(1)
local Tween = game:GetService("TweenService"):Create(Fade,Info,{BackgroundTransparency=0})
Tween:Play()
Tween.Completed:Wait()
end)
You’re only referencing the player’s character once (the script only runs once since you have ResetOnSpawn disabled). After they die, it isn’t referenced again, therefore the connection won’t work. Try using player.CharacterAdded.
This works, but now there seems to be an opposite-version of the original problem occurring. It doesn’t play the first time the player died, but instead it plays every time after. Do you know what could be causing this?
The exact Script:
local player = game.Players.LocalPlayer
local Fade = script.Parent.Frame
player.CharacterAdded:Connect(function(char)
char:WaitForChild('Humanoid').Died:Connect(function()
Fade.Visible = true
--The first tween
local Info = TweenInfo.new(1)
local Tween = game:GetService("TweenService"):Create(Fade,Info,{BackgroundTransparency=0})
Tween:Play()
Tween.Completed:Wait()
wait(2)
--The second Tween
local Info2 = TweenInfo.new(1)
local Tween2 = game:GetService("TweenService"):Create(Fade,Info2,{BackgroundTransparency=1})
Tween2:Play()
Tween2.Completed:Wait()
wait(2)
Fade.Visible = false
end)
end)
local function WaitForDeath(char)
char:WaitForChild('Humanoid').Died:Connect(function()
...
end)
end
local char = player.Character or player.CharacterAdded:Wait()
WaitForDeath(char)
player.CharacterAdded:Connect(WaitForDeath)