This script is supposed to display a death message on a TextLabel when somebody dies, but when someone dies, nothing happens to the TextLabel
local pl = game:GetService('Players')
local teams = game:GetService('Teams')
local spectateTeam = teams.Spectator
local GUI = game.StarterGui.ScreenGui.DeathMessage
local function ResetMessage()
wait(7)
GUI.Text = " "
end
pl.PlayerAdded:Connect(function(player)
while true do
local character = player.Character or player.CharacterAdded:Wait()
character.Humanoid.Died:Connect(function()
player.Team = spectateTeam
GUI.Text = player.Name.." died"
ResetMessage()
end)
wait()
end
end)
You don’t need a loop; you are just constantly creating a connection, which would just cause some issues for the server once the player died.
local function PlayerDied(player)
local deathMessage = Player.PlayerGui.ScreenGui.DeathMessage
player.Team = spectateTeam
deathMessage.Text = player.Name .. " died"
task.wait(7)
deathMessage.Text = ""
end
local function ConnectToPlayerDied(character)
charater:WaitForChild("Humanoid").Died:Connect(function()
PlayerDied(player)
end)
end)
Player.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
ConnectToPlayerDied(character)
player.CharacterAdded:Connect(ConnectToPlayerDied)
end)
Edit: Honestly, you don’t even need to do the death message on a server script to lessen the stress on the server; all you need from the server is to set the player team since this should be global, while the death message should only be handled by a client script since only the client can really see this.
No, you can do all of that from the client simply use local player and the rest should be the same except for the team:
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
ConnectToPlayerDied(character)
player.CharacterAdded:Connect(ConnectToPlayerDied)
local pl = game:GetService('Players')
local teams = game:GetService('Teams')
local spectateTeam = teams.Spectator
pl.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
player.Team = spectateTeam
for i,v in pairs(Players:GetPlayers()) do
if v:FindFirstChild("PlayerGui") and v.PlayerGui:FindFirstChild("ScreenGui") and v.PlayerGui.ScreenGui:FindFirstChild("DeathMessage") then
v.PlayerGui.ScreenGui.DeathMessage.Text = player.Name.." died"
end
end
task.wait(7)
for i,v in pairs(Players:GetPlayers()) do
if v:FindFirstChild("PlayerGui") and v.PlayerGui:FindFirstChild("ScreenGui") and v.PlayerGui.ScreenGui:FindFirstChild("DeathMessage") then
v.PlayerGui.ScreenGui.DeathMessage.Text = ""
end
end
end)
end)
end)