Death Screen GUI not repeating

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)

ResetOnSpawn is set to false.

1 Like

You need to make this a function then connect it to the Humanoid:Died function

1 Like

Like

local function DeathScreen()
 -- tween code here
end

Humanoid.Died:Connect(DeathScreen)
1 Like

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

1 Like

Oh right I see, it is in a ScreenGui with ResetOnSpawn disabled. Where would be a suitable place for the script?

1 Like

Wouldn’t a ScreenGui related to death in this case need ResetOnSpawn enabled so it starts over?

If you require it disabled for other reasons, you cna put it as a Child of StarterGui and change some of the code around

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)
1 Like

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.

player.CharacterAdded:Connect(function(char)
    char:WaitForChild('Humanoid').Died:Connect(function()
        ...
    end)
end)

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)

I had a feeling that would happen. Try this:

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)

2 Likes

This worked, thank you a lot for your help! :smiley:

1 Like

did you even tried mine?char limit____

1 Like