Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make GUI appear on player death?

I've tried this script and it doesn't work! All help appreciated. (this is a local script btw)

game.Players.PlayerAdded:Connect(function(plr)
	if plr.Humanoid.Health == 0 then
		script.Parent.Visible = true
		wait(2)
		script.Parent.Visible = false
	end
end)

2 answers

✓ Accepted answer

That is actually a very ineffective method. The reason is because this would take up more processing power. Use an event to check the player’s health (put a local script in StarterCharacterScripts):

script.Parent.Humanoid.GetPropertyChangedSignal(“Health”):Connect(function() —A check for the health changing
	if script.Parent.Humanoid.Health < 1 then —If they died
		—Make the GUI	

There is an easy way to insert a GUI into the player, and that is to reference a GUI object in ReplicatedStorage (I will explain why in a bit) and then insert it into the player. So right before our event, define the following variables:

local replicatedStorage = game:GetService(“ReplicatedStorage”) —It is good practice to use GetService instead of referencing it through game
local players = game:GetService(“Players”)
local deathGui = replicatedStorage:WaitForChild(“NameOfYourGuiHere”)
local player = players.LocalPlayer —Get the player for the GUI part of the script
—Our code to check for player’s death is on this line

Now, we simply replicate it into the GUI. The default respawn time for a player is 5 seconds, so we will wait five seconds before the GUI disappears:

	if script.Parent.Humanoid.Health < 1 then
		—Insert our new code here
		local playerGui = player.PlayerGui ——This is where we insert the gui
		local clonedGui = deathGui:Clone()
		clonedGui.Parent = playerGui —Make it so the player can see it

		wait(5)

		clonedGui:Destroy() —Destroy the GUI

Now, just close out the if statement and event

	end
end)

Now, the reason why we put the GUI in ReplicatedStorage instead of ServerStorage, is because the items stored in ReplicatedStorage can be accessed by both server and client, while ServerStorage is only accesible by server scripts. Please note that the reason I could not put actual comments is because iPad won’t let you for some strange reason.

Hope this helped!

You forgot to enclose the if statement in a loop:


while wait() do
	if plr.Humanoid.Health == 0 then
		-- blah blah blah
	end
end

You should use the CharacterRemoving event though -- it'll prove much more reliable.


-- LocalScript

local player = game.Players.LocalPlayer

player.CharacterRemoving:Connect(function()
	script.Parent.Visible = true
	wait(2)
	script.Parent.Visible = false
end)

1 comment

0

-1 for suggesting polling over events.

This discussion is preserved as it was. New answers, comments, and votes are closed.