local gui = game.ReplicatedStorage.guis.inviteplr

for i,v in pairs(game.Teams.Spectators:GetPlayers()) do
	print("script ran")
	local clone = gui:Clone()
	clone.Player.Value = v.Name
	clone.Parent = script.Parent
end

game.Players.PlayerAdded:Connect(function(player)
	local clone = gui:Clone()
	clone.Player.Value = player.Name
	clone.Parent = script.Parent
	player:GetPropertyChangedSignal("TeamColor"):Connect(function()
		if player.TeamColor ~= BrickColor.new("Dark stone grey") then
			for i,v in pairs(script.Parent:GetChildren()) do
				if v:FindFirstChild("Player") then
					if v.Player.Value == player.Name then
						print("player not in team, destroying ("..player.Name..")")
						v:Destroy()
					end
				end
			end
		else
			print("player in team, adding")
			local clone = gui:Clone()
			clone.Player.Value = player.Name
			clone.Parent = script.Parent
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs(script.Parent:GetChildren()) do
		if player.Name == v.Player.Value then
			print("player leaving, destroying")
			v:Destroy()
		end
	end
end)

the script is supposed to function when a player joins and when the value changes. the value is a string value which contains a player's username.

right now, the script is only able to detect when players that joined after the local player changes teams, but i need it so that it is every player on the server.