✓ 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!
1 comment
-1 for suggesting polling over events.