Why is there a delay in my Death Gui?

local plrs = game.Players

plrs.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char:FindFirstChild(“Humanoid”).Died:Connect(function()
local pg = plr:WaitForChild(“PlayerGui”)
repeat wait() until plr:FindFirstChild(“PlayerGui”)
pg.DeathFrameGui.Frame.Visible = true
script.DeathSound:Play()
wait(10)
pg.DeathFrameGui.Frame.Visible = false
end)
end)
end)

This is a script for a Death Gui but it comes after players respawn and not immediately, did I do something wrong in script?

1 Like

1 Like
local plrs = game:GetService("Players")

plrs.PlayerAdded:Connect(function(plr)
    plr.CharacterRemoving:Connect(function()
        local pg = plr:WaitForChild("PlayerGui")
        pg.DeathFrameGui.Frame.Visible = true
        script.DeathSound:Play()
        task.wait(10)
        pg.DeathFrameGui.Frame.Visible = false
    end)
end)

try CharacterRemoving

1 Like


There is an error with this script

1 Like

my bad use these double quotes instead

“PlayerGui”

1 Like

Wdym double quotes? and do I replace my script or add on to it?

1 Like

these are double quotes

""

I used the wrong ones and you had an error

1 Like

Sorry I see what you mean now…

1 Like

Yea there is still a delay and the Gui stays up for too long, I put 10 seconds because it automatically resets when character respawns but now it actually stay up for 10 seconds…

1 Like

It also appears when you switch teams…

1 Like

the code should only run if the character instance is removed, it shouldn’t run code when you switch teams

but if there is still a delay I guess you can try using Humanoid:GetPropertyChangedSignal(“Health”)

1 Like

Try this:

local plrs = game.Players

plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:FindFirstChild("Humanoid").Died:Connect(function()
			local pg = plr:WaitForChild("PlayerGui")
			repeat wait() until pg
			pg.DeathFrameGui.Frame.Visible = true
			wait(10)
			pg.DeathFrameGui.Frame.Visible = false
		end)
	end)
end)
1 Like

where do add this in the script?

1 Like

There’s a delay because you are asking the game to repeat wait until playergui exists. Instead, you can do this:

   local plrs = game.Players

    plrs.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
    char:FindFirstChild(“Humanoid”).Died:Connect(function()
    local pg = plr:WaitForChild(“PlayerGui”)
  --  repeat wait() until plr:FindFirstChild(“PlayerGui”)
local playerGui = plr:WaitForChild("PlayerGui")
    pg.DeathFrameGui.Frame.Visible = true
    script.DeathSound:Play()
    wait(10)
    pg.DeathFrameGui.Frame.Visible = false
    end)
    end)
    end)

You may need to remove an end

1 Like

There is still a delay and I cant hear sound anymore.

1 Like

There is an error

1 Like

Yeah my bad, keep the end at the end

EDIT: Just realized you already have a player gui variable. You can remove the one I added, and just remove the repeat. it SHOULD work

2 Likes

Why are you even using wait for a death overlay? The default respawn time is 5 seconds and event-based programming is staring right at you, so you should be trying to use events to control the visibility state of your Gui (depending on if ResetOnSpawn is enabled or not).

If your Gui has ResetOnSpawn then you don’t need to control the visibility of the Gui yourself. If it does then your Died connection should only be making the death overlay visible and your CharacterAdded connection should be making it invisible.

There are some additional recommendations I’d like to make:

  • Please condense your posts. DevForum isn’t a real time chat channel. Posting multiple short posts is sort of spammy and bloats replies as well as the time to scroll through information. You can add extra related thoughts on to a previous post with an edit.

  • Debug first when something doesn’t work and try fixing the problem yourself rather than trying to get others to fix it every time it errors. If it does error and you can’t figure it out and need help, you can reply, but provide context. The console is available to report issues in testing modes and for red outlines you can hover over them to see what problem they’re reporting.

1 Like

this worked but the gui still stays up for 10 seconds

1 Like

Probably cause you have a wait(10)


plrs.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:FindFirstChild("Humanoid").Died:Connect(function()
			local pg = plr:WaitForChild("PlayerGui")
			pg.DeathFrameGui.Frame.Visible = true
			wait(10)
--Change the wait to how long you want it to stay up (in seconds)
			pg.DeathFrameGui.Frame.Visible = false
		end)
	end)
end)
2 Likes