What do you want to achieve? Keep it simple and clear!
Make GUI disappear after time limit
What is the issue? Include screenshots / videos if possible!
If the player respawns, the GUI wont be disabled
Also the GUI appears for the first player then the second because of the timer. I dont know how i would fix that.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Dev fourms
Code:
script.Parent:FindFirstChild("Humanoid").Died:Connect(function()
for i, player in pairs(game:GetService("Players"):GetPlayers())do
player.PlayerGui:WaitForChild("NotificationUI").Enabled = true
if script.Parent:FindFirstChild("IsNPC") and player and player.Character then
player.PlayerGui:WaitForChild("NotificationUI").NotificationFrame.NotificationLabel.Text = "NPC" ..script.Parent.Name.. " Has Died!"
else
player.PlayerGui:WaitForChild("NotificationUI").NotificationFrame.NotificationLabel.Text = "Player " ..script.Parent.Name.. " Has Died!"
end
task.wait(2)
player.PlayerGui:WaitForChild("NotificationUI").Enabled = false
end
end)
script.Parent:FindFirstChild("Humanoid").Died:Connect(function()
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
-- Make sure the player has a PlayerGui and NotificationUI
local playerGui = player:FindFirstChild("PlayerGui")
if playerGui then
local notificationUI = playerGui:FindFirstChild("NotificationUI")
if notificationUI then
-- Show the notification
notificationUI.Enabled = true
notificationUI.NotificationFrame.NotificationLabel.Text =
(if script.Parent:FindFirstChild("IsNPC") then "NPC " else "Player ") .. script.Parent.Name .. " Has Died!"
task.wait(2)
-- Get the UI again in case the player respawned
local updatedUI = playerGui:FindFirstChild("NotificationUI")
if updatedUI then
updatedUI.Enabled = false -- Hide the notification after 2 seconds
end
end
end
end
end)
When a player dies and respawns, Roblox resets their screen (PlayerGui). This means the old notification you turned on disappears, and a new one gets made. But your script is still trying to turn off the old one, which doesn’t exist anymore. So, after waiting, the new notification stays on instead of disappearing.
use task.delay() so it appears for all players at the same time
script.Parent:FindFirstChild("Humanoid").Died:Connect(function()
for i, player in pairs(game:GetService("Players"):GetPlayers())do
player.PlayerGui:WaitForChild("NotificationUI").Enabled = true
if script.Parent:FindFirstChild("IsNPC") and player and player.Character then
player.PlayerGui:WaitForChild("NotificationUI").NotificationFrame.NotificationLabel.Text = "NPC" ..script.Parent.Name.. " Has Died!"
else
player.PlayerGui:WaitForChild("NotificationUI").NotificationFrame.NotificationLabel.Text = "Player " ..script.Parent.Name.. " Has Died!"
end
task.delay(2,function()
player.PlayerGui:WaitForChild("NotificationUI").Enabled = false
end)
end
end)
script.Parent:FindFirstChild("Humanoid").Died:Connect(function()
for i, player in pairs(game:GetService("Players"):GetPlayers())do
player.PlayerGui:WaitForChild("NotificationUI").Enabled = true
if script.Parent:FindFirstChild("IsNPC") and player and player.Character then
player.PlayerGui:WaitForChild("NotificationUI").NotificationFrame.NotificationLabel.Text = "NPC" ..script.Parent.Name.. " Has Died!"
else
player.PlayerGui:WaitForChild("NotificationUI").NotificationFrame.NotificationLabel.Text = "Player " ..script.Parent.Name.. " Has Died!"
end
print("enabled")
task.delay(2,function()
player.PlayerGui:WaitForChild("NotificationUI").Enabled = false
print("disabled")
end)
end
end)
game:GetService("Players").PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Died:Connect(function()
-- Notify all players
for _, otherPlayer in pairs(game:GetService("Players"):GetPlayers()) do
local playerGui = otherPlayer:FindFirstChild("PlayerGui")
if playerGui then
local notificationUI = playerGui:FindFirstChild("NotificationUI")
if notificationUI then
-- Enable notification
notificationUI.Enabled = true
notificationUI.NotificationFrame.NotificationLabel.Text = "Player " .. player.Name .. " Has Died!"
end
end
end
-- Wait 2 seconds, then disable the notification for all players
task.wait(2)
for _, otherPlayer in pairs(game:GetService("Players"):GetPlayers()) do
local playerGui = otherPlayer:FindFirstChild("PlayerGui")
if playerGui then
local notificationUI = playerGui:FindFirstChild("NotificationUI")
if notificationUI then
notificationUI.Enabled = false
end
end
end
end)
end
end)
end)
This has worked for me, please make sure it is a script in serverscriptservice
I couldn’t help but notice that the solutions are very long, and keeping track of each player on the server isn’t advisable. This can be solved by using a simple client-server - server-clients solution.
Using the server to enable UI for a player is also unnecessary, as the client is usually in charge of visuals, and it puts strain on the server as well. (Again, use the client-server server-client method. Make sure to do checks though!)
To answer your question of the UI resetting on respawn, just check the ResetOnSpawn (In the ScreenGui class) property to false.