How to check if a player died or not issues

Hello, I would like to know, how can I check if a player died once or not?

I want that if the player died a boolValue of dead is marked, and after this make a gui text can be seen only for him (only dead player with boolValue activated can see that gui text).

How can I do this? Should I use datastorage? I would like to know ideas about it.

In short, I want only the player who died to be able to see something specific.

Nah, a DataStore Service shouldn’t be necessary

The most simplistic way of approaching this is by defining the Player’s GUI inside a server script using a PlayerAdded event, and detecting when the Character dies using the Humanoid.Died event

Even so, BoolValues I don’t believe are necessary since the Humanoid.Died event will fire as soon as the Character’s Model dies

game.Players.PlayerAdded:Connect(function(Player)
    local PlayerGui = Player:WaitForChild("PlayerGui")

    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")

        Humanoid.Died:Connect(function()
            PlayerGui.GuiToEnable.Enabled = true
        end)
    end)
end)

Of course you can also set the ResetOnSpawn property of the GuiObject to true if you want it to reset upon the Character respawning

4 Likes

Be careful, the character can load before CharacterAdded is registered

However, couldn’t you just do a LocalScript? Why use a Server Script?
Something like this in StarterCharacterScripts

local Humanoid = script.Parent:WaitForChild("Humanoid")

Humanoid.Died:Connect(function()
  ---enable the gui and stuff here
end)

Wow, amazing! Thanks you man! and what about if I need it to become false after a certain time, how can I do it?

How could that be fixed? :scream:

I’m assuming you meant the GuiObject? If so, then you could set a wait() duration for how long you want it to be enabled for, then set its Enabled property to false

I’d recommend you not use PlayerAdded as it’s unnecessary and UI Code & Effects should always be done on the Client (to prevent unnecessary replication), instead use something similar to my StarterCharacterScripts example

Eh it’s force of habit, even still both would work either way

Just that I’d prefer using PlayerAdded as long as you’re not handling too much on UI objects

Thank you very much for providing me with your code, this works! now I’m going to save it in datastorage, I know how to do it, thanks for everything! :smiley:

*By the way, should I fix something here if I add data storage?

game.Players.PlayerAdded:Connect(function(Player)
	local dead = Instance.new("BoolValue", Player)
	dead.Name = "IsDead"
	dead.Value = false
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")
		Humanoid.Died:Connect(function()
			dead.Value = true
		end)
	end)
end)

Not sure what you mean by that :thinking: But you could just simply call the SetAsync() function for the DataStoreService to save the current value of that BoolValue every couple of seconds, or whenever the Humanoid.Died Event is fired

In case if i want the boolValue to be saved, would it be good if i set it to false from playerAdded?

By default, BoolValues are set to false when creating one so there’s really no need to be setting it at all

You could just set the dead.Value back to true after 5 seconds have passed (Since that’s the default respawn time when players respawn back in, unless you have specific instances where you prevent the character from loading)

1 Like

I understand, thank you very much for the clarification and your answer, thank you! and thank you all :smiley:

1 Like