Why does this script not do anything to the TextLabel?

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)
  1. Where is your script located specifically?
  2. You can’t access local GUI = game.StarterGui.ScreenGui.DeathMessage locally.

Ohhhhhhh. This is stored in ServerScriptService. Would I have to put this in the TextLabel itself?

1 Like

Why is the script inside a while loop? Also of course it won’t work because you are using the StarterGui not the player’s GUI.

1 Like

I put it in a while loop so it constantly checks for a player death. Is a loop not required for that?

Yes. You can’t access local UI from the server. You need to place it in the UI inside a Local Script, so that each client runs this locally.

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.

1 Like

Could I end up sending a RemoteEvent when the player dies, pass the player info through it, then modify the TextLabel through a local script?

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)
1 Like

Is Player here game.Players:GetPlayers()?

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)

This would be a ServerScript right?

Yes, the script I showed you would be a server script.

Alright. The script works. Thank you very much for helping me out with this!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.