Help re adding highlight to dead players

Hello, in my game I’m trying to make a spy that highlights players for a brief moment. I’m currently stuck when trying to re ad the highlight if a highlighted player dies. How would I use the CharacterAdded event? I’m pretty new to scripting.

local function spy()
	if spy then
		for _, player in pairs(game.Players:GetPlayers()) do
			local highlight = Instance.new("Highlight")
			highlight.FillColor = player.TeamColor.Color
			highlight.FillTransparency = 0.6
			highlight.OutlineColor = player.TeamColor.Color
			highlight.Parent = player.Character
		end
	elseif not spy then
		for _, highlight1 in pairs(game.Players:GetPlayers().Character) do
			highlight1:Destroy("Highlight")
		end
	end
end
1 Like

here’s a script that should work, I threw this together in vsc, so it may not be 100% accurate

for i,player in next, game:GetService("Players"):GetPlayers() do
    local Character = player.Character
    
    if Character then
        local highlight = Instance.new("Highlight")
        highlight.FillColor = player.TeamColor.Color
        highlight.FillTransparency = 0.6
        highlight.OutlineColor = player.TeamColor.Color
        highlight.Parent = Character
    end

    player.CharacterAdded:Connect(function(Character)
        local highlight = Instance.new("Highlight")
        highlight.FillColor = player.TeamColor.Color
        highlight.FillTransparency = 0.6
        highlight.OutlineColor = player.TeamColor.Color
        highlight.Parent = player.Character
    end)
end
1 Like

Thanks! I’ve never used in next before. I’ll have a look at some documentation