How to make a gui disappear on death and come back when you respawn?

Hey there! So I am trying to add an objective system to my horror game, and I want the objective gui text to disapear when the player die, but come back on the screen when they respawn. The script I used made the gui disapear on death, but it did not come back when I respawned. If y’all could figure this out, that would be great!

Script:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

while wait() do
	if hum.Health <=0 then
		script.Parent.Visible = false
	else
		script.Parent.Visible = true
	end
	

end
1 Like

This may take a bit of explanation because there’s a lot to unpack here.

For starters, avoid using while loops for tasks such as these. There’s a whole other thing that you can use called events, which would not only simplify your code more but also make it A LOT more optimized.

Next, if this code is not running upon respawning, it means that the script itself is getting destroyed. The executing code within scripts (loops are commonly used as such) will cease once the script has been destroyed (don’t take this as THE way to stop loops, read about them when you get the chance).

What you need to do is listen in on the humanoid.HealthChanged signal.

humanoid.HealthChanged:Connect(function()
    local isDead = humanoid.Health <= 0
    if isDead then
        -- code
    else
        -- more code
    end
end)

Now instead of 30 times a second, it’ll only check for it when the player’s health changes. You can go even further and instead listen in on the humanoid.Died event, but for simplicity’s sake we’ll stick with this.

Now you need to focus on the script being destroyed. This case makes me want to assume that the script is within the player’s gui, so check the ScreenGui’s ResetOnSpawn property and make sure that’s false. If the script is not within the player’s gui then there’s something else that’s destroying it.

Lastly, now that your script runs even if your character respawns, you need to reconnect this event every time the player gets a new character. You can find out how to connect your code through this post, which should show you about connecting through player and character events to execute code on them properly.

EDIT: I want to clarify that the answer I gave to your issue about the code not running on future characters was because of the assumption that the script itself was getting destroyed. If that is not the case, it’s really all just because that char parameter is always going to be the first character and not future characters. Just look through the last paragraph I’ve given, which should solve your problem there.

The script is in a screen gui, with under a text label. The screen gui has ResetOnSpawn off. I reckoned that spawning in a new character was the issue, but did not know how to work around it. I think the char parameter was the problem

1 Like
local Player = game:GetService("Players").LocalPlayer
local UI = script.Parent

Player.CharacterAdded:Connect(function(Char)
    UI.Visible = true 

    Char:WaitForChild("Humanoid").Died:Once(function()
        UI.Visible = false
    end)
end)

1 Like

I mean that definitely was the most direct problem yeah, glad we got over that! But please consider everything else I mentioned aside from the script destroying stuff, because it’ll be pretty important down the line when you want to work on cooler stuff!

Of course, thanks for the info! Trying to add the previous post’s content into the script to see if it works

1 Like

Awesome and no problem! Feel free to reply here with your code again if you’re having issues hooking up stuff from the other post :slight_smile:

It works now! I used Czo’s script to properly make sense of how to put it into the code. I understand it a lot more now. Who should I give the solution to though?

One small tweak at the end here, you should throw that code into a function and run it after the connection to cover the case of the connection happening after the first character spawns:

function OnCharacter(char)
    UI.Visible = true 

    Char:WaitForChild("Humanoid").Died:Once(function()
        UI.Visible = false
    end
end

-- connect to the event
Player.CharacterAdded:Connect(OnCharacter)

-- check if our character already exists
local existingCharacter = Player.Character
if existingCharacter then
    task.spawn(OnCharacter, existingCharacter)
end
-- code past here will not yield to OnCharacter since it's in a task.spawn()

But other than that it looks good! Thanks for sharing that @CZXPEK :sunglasses:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.